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.

Scrambling a word/line (please help)

Old posts that have not been replied to for several years.
Locked
F
FreakyComputer
Voice
Posts: 22
Joined: Fri May 10, 2002 8:00 pm
Location: South Africa

Post by FreakyComputer »

Okay, this is the part I am stuck with: i have a word ie. guranga in $ogaword but now i want my script to scramble, thus making that word ie. gaanrug using ALL the letters in the $ogaword and scrambling it... If anyone can just give that little snippet of code

Thanks
FreakyComputer
User avatar
slennox
Owner
Posts: 593
Joined: Sat Sep 22, 2001 8:00 pm
Contact:

Post by slennox »

This is adapted from my proc for randomising lists rather than strings, so it may not be the most efficient solution, but it works:

Code: Select all

proc randstring {string} {
  set randstring ""
  while {[string length $string] > 0} {
    set random [rand [string length $string]]
    append randstring [string index $string $random]
    set string [string range $string 0 [expr {$random - 1}]][string range $string [expr {$random + 1}] end]
  }
  return $randstring
}
randstring <string>
F
FreakyComputer
Voice
Posts: 22
Joined: Fri May 10, 2002 8:00 pm
Location: South Africa

Post by FreakyComputer »

Thanks you slennox :smile: That piece of code is really what my script needed, so now my string is always scrambled! Thank you :smile:)

FreakyComputer
Locked