in reply to Randomize word

In the spirit of TIMTOWTDI (mostly because I'm not sure if List::Util is in the core distribution), I offer this snippet of code:

#!/usr/bin/perl use strict; my @word_array = split '', "iguana"; fisher_yates_shuffle(\@word_array); print "Word is ", join ('', @word_array), "\n"; # From perldoc -q shuffle: sub fisher_yates_shuffle { my $deck = shift; # $deck is a reference to an array my $i = @$deck; while ($i--) { my $j = int rand ($i+1); @$deck[$i,$j] = @$deck[$j,$i]; } }

I would like to know if there's a simple way to remove the extraneous @word_array variable. Maybe I'm just overtired, but I couldn't see a way to get an array reference from the return of the split...

Replies are listed 'Best First'.
Re^2: Randomize word
by BrowserUk (Patriarch) on Sep 07, 2004 at 13:18 UTC

    Your tired and gonna kick yourself :)

    print "Word is: ", join '', @{ fisher_yates_shuffle( [ split '', "iguana" ] ) };

    Of course, you'd have to return the reference passed in though.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
      You are correct on the tired bit. And thanks for the correct syntax -- must have been a mental block on this when I was looking at it earlier.

      I do remember trying bizarre things like @( split '', "iguana") and I'm not sure why I thought that would work. I suppose I should just stick to my original overtired excuse. I was, after all, looking at the sunrise from the wrong side (as in, I was up all night, and went to bed shortly after posting this).

        We all have nights like that--least I do :0 (that's a yawn!).

        Funny thing with me is that there is a sweet spot between getting tired, and becoming incapable. It varies between 1/2 hour and a couple of hours in which everything just seems to click. I can be more productive in that time than throughout the rest of the day. The problem is recognising when I transition from the state of grace to "what a state". It's not always obvious to me at the time, as there are a few indicators around this site.


        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "Think for yourself!" - Abigail
        "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon