Well, I'm still new to perl, so its a nice and short code that takes a .txt file and reads line by line removing whitespace adding each character into an array. Then it randomly chooses a character in the array, adding that character to a new array all while replacing it with whitespace in the old as a nice way to filter out the chance of the random num coming back to the whitespace. Well its all good fun. Helped me out with arrays it did!
#!usr/bin/perl while(<>) { chomp; s/\s//g; #Remove whitespace within the line $leng = length($_); $count = -1; while ($count < ($leng - 1) ) { $count ++; $char = substr($_,$count, 1); push @list, $char; #Push one letter into the array } } print "@list\n"; my @list2; while ( $#list2 < $#list) { $randint = int(rand($#list+1)); redo if $list[$randint]=~" "; push @list2, $list[$randint]; $list[$randint]=" "; } print "@list2\n";
Enjoy! **UPDATED BASED ON REPLIES
#!usr/bin/perl push @list, grep{/\S/} split '' while <>; print "@list\n"; my @list2; while (@list){ $randint = int(rand($#list+1)); push @list2, splice @list, $randint, 1; } print "@list2\n";
Thanks for all the great ideas!

Replies are listed 'Best First'.
Re: Array Fun
by bart (Canon) on Apr 06, 2007 at 11:06 UTC
    Then it randomly chooses a character in the array, adding that character to a new array all while replacing it with whitespace in the old as a nice way to filter out the chance of the random num coming back to the whitespace.
    Well there's a different approach you can take for this, and more Perlish, too. And that is to just remove the character from the array with splice.

    It has the advantage that you won't have a possibly slowdown near the end, when the array is almost only filled with spaces, to find that last non-space ones.

    And here is how:

    Another cheaty way, is to look at shuffle in List::Util.

    The latter uses the so-called Fisher-Yates shuffle, and if you look very closely, you'll find that both are entirely equivalent, only, Fisher-Yates reuses a (growing) part of the original array to hold the items for your @list2, as the room left for the original items in the same array (your @list) shrinks.

      Interesting stuff! The module would have made it really easy. Gonna try to use the splice function a bit. Thanks for the feedback!
Re: Array Fun
by wfsp (Abbot) on Apr 06, 2007 at 10:38 UTC
    phantom20x++

    Nifty stuff!

    Are you up for a challenge? Try replacing your first while loop with

    push @list, grep{/\S/} split '' while <>;
    What's going on there? Have a look at split and grep and see if you can work it out. Get back to us if you need a pointer.

    Happy Perling!

      Thanks, thats really cool. Haven't looked into split or grep, but it seems like quite a bit can be done with them. So in this case you use grep to pattern search for any non-whitespace character and then it splits them. Still trying to understand split but I think I get the grep part.
      don't forget map and sort beside split and grep. ;)
      my @list2 = map {$_[0]} sort {$b[1] <=> $a[1]} map {[$_,rand]} @list;
Re: Array Fun
by jwkrahn (Abbot) on Apr 18, 2007 at 13:49 UTC
    Another way to do the same thing:
    use List::Util 'shuffle'; my @list = do { local $/; <> =~ /\S/g }; print "@list\n"; my @list2 = shuffle @list; print "@list2\n";