Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Stuck on a Perl assignment

by stevieb (Canon)
on Jan 26, 2017 at 00:37 UTC ( [id://1180351]=note: print w/replies, xml ) Need Help??


in reply to Stuck on a Perl assignment

Along with having an array reference as opposed to a hash reference, ref($aref) returns ARRAY, not LIST, so that'll never catch.

Here's a basic example for review and to do some testing with:

:
use warnings; use strict; my $struct = [ { 1 => 'a', 2 => 'b', }, [ 1, 2, 3, ], { 3 => 'c', 4 => 'd', }, ]; thing($struct); sub thing { my ($aref) = @_; for my $elem (@$aref){ if (ref $elem eq 'ARRAY'){ print "array:\n"; print "$_\n" for @$elem; } elsif (ref $elem eq 'HASH'){ print "hash:\n"; print "$_ => $elem->{$_}\n" for keys %$elem; } else { die "element is not an allowed ref\n"; } } }

Output:

hash: 1 => a 2 => b array: 1 2 3 hash: 3 => c 4 => d

Log In?
Username:
Password:

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

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

    No recent polls found