SPUG: deferencing a hash in an array of hashes

Todd Wells toddw at wrq.com
Mon Nov 6 19:54:45 CST 2000

Yes, as I've discovered thanks to several kind SPUGgers, the debugger output
is what had me fooled -- in reality I did it exactly right the first time.
My eyes told me I was looking at a flat list rather than key-value pairs.  

Thanks to all who helped :-)

-----Original Message-----
From: Alex Leites [mailto:Alex.Leites at esca.com]
Sent: Monday, November 06, 2000 5:43 PM
To: Todd Wells; 'SPUG'
Subject: RE: SPUG: deferencing a hash in an array of hashes

> I have a subroutine which builds up an array of hashes
> in @attributes, and then I want to use one of those 
> hashes as it's returned value.
>   return $attributes[0]; # returns the hash reference in the array.

... just as expected, since the array of hashes is,
strictly speaking, the array of hash references.

> Alas!  This doesn't do what I want, now:
>   DB<6> x %hash
> 0  'ishidden'
> 1  'false'
> 2  'isreadable'
> Can someone please show me the error of my ways?

There is no error, this does do what you want. 
You did not explain why you think this is wrong ...

Is that because you expected %hash to be printed as before,
with key => value format? Well, perl debugger won't do that
for "simple" hashes ("simple" meaning that there are
no references among the values) -- the other format is
reserved for more complicated data structures.
Try 'x \%hash' if you want to cheat. Or use Data::Dumper.

The only other potential source of confusion, as I see it,
is that any hash can (and evaluated in list context, will)
be regarded as an array with an even number of elements --
-- but this has already been addressed in other replies.

Alex Leites

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
  Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/

More information about the spug-list mailing list