Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: hashref with(out) arrays

by tangent (Parson)
on Nov 21, 2017 at 00:09 UTC ( [id://1203856]=note: print w/replies, xml ) Need Help??


in reply to hashref with(out) arrays

You could just use ref
my $item1 = { result => { value => 'a' }, }; my $item2 = { result => [ { value => 'b' }, { value => 'c' }, ], }; parse_result( $item1->{'result'} ); parse_result( $item2->{'result'} ); sub parse_result { my $result = shift; if ( ref $result eq 'HASH' ) { print "Parsing hashref\n"; print "Value: $result->{'value'}\n"; } elsif ( ref $result eq 'ARRAY' ) { print "Parsing arrayref\n"; my @values; for my $item ( @$result ) { push( @values, $item->{'value'} ); } print "Values: @values\n"; } }
OUTPUT Parsing hashref Value: a Parsing arrayref Values: b c

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (6)
As of 2024-04-18 20:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found