This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

How to rearange data

Old posts that have not been replied to for several years.
Locked
O
Ofloo
Owner
Posts: 953
Joined: Tue May 13, 2003 1:37 am
Location: Belguim
Contact:

How to rearange data

Post by Ofloo »

i wana rearange a string backwards ex: adcb to bcda how would i do this ? without loozing data.

ive checked lsort but it seems that this command hasn't this option..
XplaiN but think of me as stupid
s
spock
Master
Posts: 319
Joined: Thu Dec 12, 2002 8:40 pm

Post by spock »

Code: Select all

proc flipstring { s } {
  set newstring ""
  set length [string length $s]
  while { [string length $newstring] != [string length $s] } {
    set newstring "$newstring[string index $s [expr $length - 1]]"
    incr length -1
  }
  return $newstring
}
there are better ways to do this so user, strikelight or ppslim might hit you up :mrgreen:
% flip adcb
bcda
% time { flip adcb } 100
1015 microseconds per iteration
maybe some native command does it for you but i assume not since you're asking
photon?
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Did someone mention my name? :P

Post by user »

This is about 7 times faster than the above (for short strings) in my tclsh:

Code: Select all

proc reverse str {
	set out ""
	foreach c [split $str ""] {set out $c$out}
	set out
}
Have you ever read "The Manual"?
O
Ofloo
Owner
Posts: 953
Joined: Tue May 13, 2003 1:37 am
Location: Belguim
Contact:

Post by Ofloo »

TnX nice i am amazed that this doesn't give an error ..
set out
???
XplaiN but think of me as stupid
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

set varName ?value?

Returns the value of variable varName. If value is specified, then set the value of varName to value...
Have you ever read "The Manual"?
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

As in is the same thing as in using "return" instead of "set" ? :)
Once the game is over, the king and the pawn go back in the same box.
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

caesar wrote:As in is the same thing as in using "return" instead of "set" ? :)
Nope...check the 'proc' manual page. :)
If the procedure doesn't execute an explicit return, then its return value is the value of the last command executed in the procedure's body.
Have you ever read "The Manual"?
O
Ofloo
Owner
Posts: 953
Joined: Tue May 13, 2003 1:37 am
Location: Belguim
Contact:

Post by Ofloo »

ic interesting .. so in a way ur saying if the variable exists , and u set it again then it returns the last executed string or the last string that was set .. basicly its the same as return but not entirely .. so this wouldn't break a loop and still return a value .. example ..
XplaiN but think of me as stupid
p
ppslim
Revered One
Posts: 3914
Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England

Post by ppslim »

Yes. This provides a nice way to access variables dynamicly.

IE

Code: Select all

set ourvar "hello"
set thisvar "ourvar"
puts -stdout [set $thisvar]
Mixed with the fact that Proc declarations will return the buffer of the last executed command, if no exclicit return is performed.

This is the precise reason you have to be careful if you are using RAW binds. Because if no return is used, the buffer from the last command is used. Thus.... you can often lose data because you forgot an explicit return.
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

Heh, nice. Thanks for pointing this out user. :)
Once the game is over, the king and the pawn go back in the same box.
Locked