Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re: Testing array of hash values

by athomason (Curate)
on Dec 11, 2001 at 07:52 UTC ( #130854=note: print w/replies, xml ) Need Help??

in reply to Testing array of hash values

Like dws, I'm surprised this would run; it doesn't for me under 5.6.1. Your declaration of %p almost has the syntax for a hash slice, except that you can't do that in a my declaration. Something like my %p; @p{ 'eyes', 'hair', 'etc' } = (); would be legitimate, if unusual.

As for why do_head doesn't do what you think, I believe you're misunderstanding the behavior of undef in an array. undef in an array or hash doesn't just disappear, it's still something. This is notably different from its behavior in a list (which is not an array!). For instance, see the behavior of this snippet:

@a = ( ); @c = ( undef ); die "a" if @a; die "b" if ( undef, undef ); die "c" if @c

Particularly, it dies with 'c', not 'a' or 'b'. In a boolean context--like an if statement--an array takes the value of its length, not its members. In my example, this is 1, which is a true value. Likewise, your three undefs give @data a true value. So, your list of undefs is in fact true when assigned to an array. You were probably expecting the behavior of a list, which in a boolean context evaluates to its last element (e.g., die "d" if ( 1, undef ); die "e" if ( undef, 1 ); dies with 'e'). The solution, if you wish to keep the behavior of printing when the elements are defined, can be found in dws' post.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://130854]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2023-09-28 11:04 GMT
Find Nodes?
    Voting Booth?

    No recent polls found