egghelp.org community Forum Index
[ egghelp.org home | forum home ]
egghelp.org community
Discussion of eggdrop bots, shell accounts and tcl scripts.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

selective whitespace trimming

 
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help
View previous topic :: View next topic  
Author Message
blotter45
Voice


Joined: 28 Feb 2005
Posts: 17

PostPosted: Sat Apr 22, 2006 12:50 am    Post subject: selective whitespace trimming Reply with quote

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
View user's profile Send private message
demond
Revered One


Joined: 12 Jun 2004
Posts: 3073
Location: San Francisco, CA

PostPosted: Sat Apr 22, 2006 1:50 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
De Kus
Revered One


Joined: 15 Dec 2002
Posts: 1361
Location: Germany

PostPosted: Sat Apr 22, 2006 2:31 am    Post subject: Reply with quote

why not simply adding the whitespace to the re, if you really love these slow things?
Code:
{ years?}

_________________
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
View user's profile Send private message MSN Messenger
blotter45
Voice


Joined: 28 Feb 2005
Posts: 17

PostPosted: Sat Apr 22, 2006 2:55 am    Post subject: Reply with quote

De Kus wrote:
why not simply adding the whitespace to the re, if you really love these slow things?
Code:
{ years?}


Actually I took your advice and applied it to string map instead of regsub, much better Wink

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help All times are GMT - 4 Hours
Page 1 of 1

 
Jump to:  
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


Forum hosting provided by Reverse.net

Powered by phpBB © 2001, 2005 phpBB Group
subGreen style by ktauber