Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: hashref with(out) arrays

by tangent (Vicar)
on Nov 21, 2017 at 00:09 UTC ( #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? | Other CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2022-05-18 12:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (70 votes). Check out past polls.

    Notices?