in reply to Randomize word

With a sufficiently recent Perl, you can let a hash randomize for you:
$_='hotel'; %f=map {$_=>1} split //; print join('',keys %f), "\n";
If your Perl isn't sufficiently recent, it will yield the same result every run. Note: this is just fun-hack solution, not a real, recommended one.

Update: I cannot imagine how I didn't notice what Anonymous Monk did. Make that:

$_='rotohotel'; my $len=0; my %f=map {$len++ => $_} split //; print join('', values %f), "\n";

Caution: Contents may have been coded under pressure.

Replies are listed 'Best First'.
Re^2: Randomize word
by Anonymous Monk on Sep 07, 2004 at 15:05 UTC
    That wouldn't work if the word to shuffle contains duplicate letters.

      Look again. Duplicate letters work.

      However, ActivePerl v5.6.1 build 633 actually returns unshuffled results for "rotohotel", "r1t2h3tel" and "r1t2h3tels"!! Nothing guarantees that order is lost in a hash.

      May I recommend a variation:

      $_='rotohotel'; my $len; my %f=map {rand() . '|' . $len++ => $_} split //; print join('', values %f), "\n";

      It seems values() is implemented as map { $hash{$_} } sort keys %hash) on this build!!

      Update: ok, so it's not always sorted:

      %hash = map { $_ => $_ } qw(m i f e z l x v b r d o h y j q s t k n g +c a p w u); print(join('', values(%hash)), "\n"); # prints: abcdefghijklmnopqrstuvwxyz SORTED %hash = map { $_ => $_ } qw(m i f e z l x 2 v b r d o h y j q s t k n +g c a p w u); print(join('', values(%hash)), "\n"); # prints: abcdefghijklmnop2qrstuvwxyz NOT SORTED %hash = map { $_ => $_ } qw(6 3 5 0 1 2 9 8 7 4); print(join('', values(%hash)), "\n"); # prints: 0123456789 SORTED %hash = map { $_ => $_ } qw(6 3 5 0 34 1 2 33 9 8 7 4); print(join(':', values(%hash)), "\n"); # prints: 0:1:2:3:4:5:6:7:8:9:33:34 SORTED %hash = map { $_ => $_ } qw(a aaaaaaa aaaa aaa aa aaaaa aaaaaaaaaa); print(join(':', values(%hash)), "\n"); # prints: aa:aaaaaaa:a:aaaaaaaaaa:aaaaa:aaaa:aaa NOT SORTED