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.

Code for word scrambling

Old posts that have not been replied to for several years.
Locked
E
Eggy

Post by Eggy »

Please anybody help
How's the code for scrambling a word?
User avatar
stdragon
Owner
Posts: 959
Joined: Sun Sep 23, 2001 8:00 pm
Contact:

Post by stdragon »

Code: Select all

set word "yummysheep"

proc scramble {word} {
  set letters [split $word ""]
  set newword ""
  while {[llength $letters]} {
    set i [rand [llength $letters]]
    append newword [lindex $letters $i]
    set letters [lreplace $letters $i $i]
  }
  return $newword
}
E
Eggy

Post by Eggy »

Thx stdragon, I'll give your code a try
E
Eggy

Post by Eggy »

Sorry to bother you again stdragon.

How can I call the procedure & say it to the channel?
E
Eggy

Post by Eggy »

I've found the way to call the procedure, but what if I want to scramble a phrase, let say I want to scramble "word1 word2 word3" and I want the result something like "rd1ow 2odwr r3owd"? dont scramble the space, keep the space at it original location.

Thanks.
User avatar
stdragon
Owner
Posts: 959
Joined: Sun Sep 23, 2001 8:00 pm
Contact:

Post by stdragon »

Code: Select all

proc scramble_phrase {phrase} {
  set newphrase [list]
  foreach word [split $phrase] {
    lappend newphrase [scramble $word]
  }
  return [join $newphrase]
}
E
Eggy

Post by Eggy »

Those both work perfectly stdragon, I do appreciate for your great help. Thanx a lot.
Locked