in reply to converting hash to shuffled array

If you only want random and not shuffled

my( $name, $url ) = do{ my @pair; # updated!! Whoops! @pair = each %db for 0 .. rand keys %db; @pair; }; print qq(<a href="$url"><img src="/images/$name"></a>);

Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
If I understand your problem, I can solve it! Of course, the same can be said for you.

Replies are listed 'Best First'.
Re: Re: converting hash to shuffled array
by sulfericacid (Deacon) on Sep 12, 2003 at 03:43 UTC
    Of all the wonderful responses I think this is the easiest one for me to understand and implement into my script. I do have a question or two about it and was wondering if you could help me figure them out.

  • Why is there a my $pair? I only see it being used in that one statement.
  • Why is there a @pair at the end when the previous line is giving it a value?

    Thanks for your help everyone! I've noticed most of your scripts are very similar in nature which makes it easier. I didn't know you could do a @array = keys %db, that must be a lot easier on the server instead of doing a foreach loop.

    "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

    sulfericacid

      Why is there a my $pair? I only see it being used in that one statement.

      Um! Cos I made a mistake! It should be @pair and now is:)

      Why is there a @pair at the end when the previous line is giving it a value?

      The previous line loops a random number of time setting the @pair to succesive key/value pairs from the hash.

      Once the loop stops, the last statement becomes the return value for the do block, and so the last key/value pair assigned to the array @pair, get assigned to the variables $name and $url.


      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
      If I understand your problem, I can solve it! Of course, the same can be said for you.