| View previous topic :: View next topic |
| Author |
Message |
blotter45 Voice
Joined: 28 Feb 2005 Posts: 17
|
Posted: Sat Apr 22, 2006 12:50 am Post subject: selective whitespace trimming |
|
|
heya guys, I'm just looking for a quick way to trim out certain whitespaces from a string. Here is a proc that just takes a seconds input, takes the [duration seconds] value, and trims it down (so weeks becomes w, days becomes d, and so on):
| Code: |
proc sr:shortdur { t } {
set t [duration $t]
regsub -all -- {years?} $t {y} t
regsub -all -- {weeks?} $t {w} t
regsub -all -- {days?} $t {d} t
regsub -all -- {hours?} $t {h} t
regsub -all -- {minutes?} $t {m} t
regsub -all -- {seconds?} $t {s} t
return $t
} |
so a duration of "2 weeks 3 days 14 minutes 36 seconds" then becomes "2 w 3 d 14 m 36 s", but then I'd like to trim the spaces between the number and w,d,m,s so that it becomes: "2w 3d 14m 36s".. So is regsub also the best way to do that, or can you recommend another method? |
|
| Back to top |
|
 |
demond Revered One

Joined: 12 Jun 2004 Posts: 3073 Location: San Francisco, CA
|
Posted: Sat Apr 22, 2006 1:50 am Post subject: |
|
|
use this:
| Code: |
proc duration {t} {
set days [expr $t / (3600 * 24)]
set hour [expr ($t - (3600 * 24 * $days)) / 3600]
set mins [expr (($t - (3600 * 24 * $days)) % 3600) / 60]
if $days {append str "$days day(s), "}
if $hour {append str "$hour hour(s), "}
return [append str "$mins minute(s)"]
}
|
_________________ connection, sharing, dcc problems? click <here>
before asking for scripting help, read <this>
use [code] tag when posting logs, code |
|
| Back to top |
|
 |
De Kus Revered One

Joined: 15 Dec 2002 Posts: 1361 Location: Germany
|
Posted: Sat Apr 22, 2006 2:31 am Post subject: |
|
|
why not simply adding the whitespace to the re, if you really love these slow things?
_________________ De Kus
StarZ|De_Kus, De_Kus or DeKus on IRC
Copyright © 2005-2009 by De Kus - published under The MIT License
Love hurts, love strengthens... |
|
| Back to top |
|
 |
blotter45 Voice
Joined: 28 Feb 2005 Posts: 17
|
Posted: Sat Apr 22, 2006 2:55 am Post subject: |
|
|
| De Kus wrote: | why not simply adding the whitespace to the re, if you really love these slow things?
|
Actually I took your advice and applied it to string map instead of regsub, much better
| Code: |
proc sr:shortdur { t } {
set t [duration $t]
set t [string map {" years" "y" " year" "y"} $t]
set t [string map {" weeks" "w" " week" "w"} $t]
set t [string map {" days" "d" " day" "d"} $t]
set t [string map {" hours" "h" " hour" "h"} $t]
set t [string map {" minutes" "m" " minute" "m"} $t]
set t [string map {" seconds" "s" " second" "s"} $t]
return $t
}
|
Thanks for the help guys! |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|