in reply to A brain twister? (how to make 2 lines->1)

perl-diddler:

If you don't have arrays & hashes in your hash, then a simple map might help:

print "{", join(", ", map { "$_=>$ahash{$}" } keys %ahash), "}\n";

...roboticus

When your only tool is a hammer, all problems look like your thumb.

Replies are listed 'Best First'.
Re^2: A brain twister? (how to make 2 lines->1)
by perl-diddler (Chaplain) on Jun 25, 2012 at 02:59 UTC
    Absolutely what I was looking for yours and the one above -- I don't know what I was doing wrong when I tried it ...oh, I remember, I was trying to use map directly with the hash -- thinking it would treat it as an array and I could get through the keys/values that way..., but map only works on array according to the manpage.

    So then I started looking at the hash specific functions...

    Too bad 'each' doesn't do what keys/values does -- I tried

    @array=each %hash;, #and @array= [each %hash];...
    but never got it to do what I wanted...sigh.

    I've used map with values before, but was thinking there should be some way to do similar with 'each'...but each is just demented...1 item at a time...w/no slurp-ability......

    And there I am with a straw trying to make it work...

      but map only works on array according to the manpage
      No, map works on lists.
      but each is just demented...1 item at a time...w/no slurp-ability
      No, each fills a specific, useful niche. You are forgetting that a hash in list context returns its keys and values:
      @array=%hash;

      Dave.

      How about

      while (my ($key, $value) = each %hash) {...}

      See each for details. The example is directly from the documentation.

      --MidLifeXis