Gossamer Forum
Home : General : Perl Programming :

Just a quick thing for anyone interested...

Quote Reply
Just a quick thing for anyone interested...
Say I have a string:

$phrase = "This is a random bunch of words which I will now jumble up";

...and I want to generate those words in a random order on every refresh...no duplicates or missing words, I want to see how you'd do it in the fewest possible lines of perl.

Mashing everything onto one line doesn't count.

Every ; or { or } means you have to start a new line.

Last edited by:

Paul: Apr 14, 2002, 4:46 AM
Quote Reply
Re: [Paul] Just a quick thing for anyone interested... In reply to
We're playing perl golf again this week, are we? You should really log onto IRC they have whole channels dedicated to this stuff.

- wil
Quote Reply
Re: [Wil] Just a quick thing for anyone interested... In reply to
IRC is crap.
Quote Reply
Re: [Paul] Just a quick thing for anyone interested... In reply to
??

Nah, a wealth of information and hackers to be found there!

- wil
Quote Reply
Re: [Paul] Just a quick thing for anyone interested... In reply to
How's this?
Code:
($p = "This is a random bunch of words which I will now jumble up") and (@w = split /\s/, $p);
(($w = $w[rand @w] and grep /^\Q$w\E$/, @p) ? redo : push @p, $w) for 0 .. $#w;
print join " ", @p;

I tried forever to make that a single line but kept getting errors because of the for loop.

--Philip
Links 2.0 moderator
Quote Reply
Re: [King Junko II] Just a quick thing for anyone interested... In reply to
Ooo.

Thats similar yet different to mine. I have a redo in there but didn't use grep.

Mine is a little longer though hmm.

Btw instead of

print join " ", @p

....

print "@p";

...will work the same way. It will space the elements and you can change the delimiter with $"

Last edited by:

Paul: Apr 15, 2002, 1:57 AM
Quote Reply
Re: [sponge] Just a quick thing for anyone interested... In reply to
Grrr...

Code:
@c = split ' ' => "This is a random bunch of words which I will now jumble up";
$r = int rand @c, exists $rand{$r} ? redo : $rand{$r}++, print "$c[$r] " for (@c);

Can't get it to one line.

Last edited by:

Paul: Apr 15, 2002, 2:31 AM
Quote Reply
Re: [Paul] Just a quick thing for anyone interested... In reply to
Create a sub to shuffle, then just call the sub with your splitted array - or is this cheating? ;-)

- wil
Quote Reply
Re: [Wil] Just a quick thing for anyone interested... In reply to
no, that's not cheating but it certainly won't earn you a very low score.

--Philip
Links 2.0 moderator
Quote Reply
Re: [sponge] Just a quick thing for anyone interested... In reply to
Alrighty then :-)

http://www.perldoc.com/...e-an-array-randomly-

- wil
Quote Reply
Re: [Wil] Just a quick thing for anyone interested... In reply to
Well this is another way....still 2 lines...ugh...but this way isn't very good.

Code:
@c = split ' ' => "This is a bunch of random stuff";
print (splice(@c, rand @c, 1)." ") while @c;

You can do it in one now I look...ah well. Im bored now.

Last edited by:

Paul: Apr 15, 2002, 3:13 AM
Quote Reply
Re: [Wil] Just a quick thing for anyone interested... In reply to
No "shuffle" routines provided in perldoc are short enough to beat us out in this game. The example using splice will not work when converted to single statement format (no {}'s ...). It seems to be a limitation in the interpreter, because it would seem that using lots of ()'s, "and"'s, and ","s to combine several statements would work.

--Philip
Links 2.0 moderator