clegane has asked for the wisdom of the Perl Monks concerning the following question:

Hey there. I have a hash that's sort of 3-dimensional. Each hash value is itself an array of strings and arrays. The hash %policies has been populated with this line:

$policies{$ruleNum} = [ $ruleName, $srcZone, [ @srcList ], $dstZone, [ @dstList ], [ @svcList ], $ruleAction ];

I'm trying to get into the embedded arrays with the following code, but the print command is giving array references. What am I doing wrong? Thank you!!!

sub get_address_policies { my $targetAddr = shift; my $targetType = shift; my $targetRole = shift; my @polMembers; foreach my $policy (keys %policies) { (lc($targetRole) eq "source") ? (@polMembers = $policies{$poli +cy}[2]) : (@polMembers = $policies{$policy}[4]); foreach my $polMember ( @polMembers ) { print "$polMember\n"; } } }

Replies are listed 'Best First'.
Re: Trying to access array within hash
by NetWallah (Canon) on Jun 03, 2013 at 21:48 UTC
    You are attempting to convert/hydrate an array REF into an array with
    @polMembers = $policies{$policy}[2]
    Try this instead:
    @polMembers = @{ $policies{$policy}[2] };

                 "I'm fairly sure if they took porn off the Internet, there'd only be one website left, and it'd be called 'Bring Back the Porn!'"
            -- Dr. Cox, Scrubs

Re: Trying to access array within hash
by ig (Vicar) on Jun 04, 2013 at 07:11 UTC

    You may have good reason for making copies of arrays in your broader application, but in the example the copies are not necessary. You might consider:

    $policies{$ruleNum} = [ $ruleName, $srcZone, \@srcList, $dstZone, \@ds +tList, \@svcList, $ruleAction ];
    sub get_address_policies { my $targetAddr = shift; my $targetType = shift; my $targetRole = shift; foreach my $policy (keys %policies) { my $polMembers = $policies{$policy}[ (lc($targetRole) eq "source") ? 2 : 4 ]; foreach my $polMember ( @{$polMembers} ) { print "$polMember\n"; } } }
Re: Trying to access array within hash
by BillKSmith (Monsignor) on Jun 04, 2013 at 12:37 UTC
    You can further simplify ig's solution with values instead of keys in the foreach and with interpolation in the print.
    foreach my $policy (values %policies) { my $polMembers = $policy->[ (lc($targetRole) eq "source") ? 2 +: 4 ]; local $" = "\n"; print "@$polMembers\n"; }
    Bill

      Thanks a bunch everyone. I got it working with Bill's solution. The reason I was copying the array is because the syntax was getting ugly, and I wasn't sure how to get things working. In Bill's solution, what does this line do exactly?

      local $" = "\n";
        see $" in perlvar

        $LIST_SEPARATOR $" This is like $, except that it applies to array and sli +ce values interpolated into a double-quoted string (or sim +ilar interpreted string). Default is a space. (Mnemonic: o +bvious, I think.)

        Are you the Hound or the Mountain? ;-)

        Cheers Rolf

        ( addicted to the Perl Programming Language)