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

I am having some issues pulling data from a complex structure: the code to build it looks like this:
foreach my $row ($te->rows) { my $count = 0; foreach my $item (@$row) { $count++; chomp($item); $$row[0] =~ s/(.*)_.*/$1/; # HOST $$row[1] =~ s/LSS: 0x0*(.*)/$1/; # LSS $$row[2] =~ s/(.*)-.*/$1/; # LUN $$row[3] =~ s/^0*(.*) GB/$1/; # Size in GB push ( @{$map{$$row[1]}{$$row[2]}}, $$row[0] ) unless $known{$ +$row[1]}{$$row[2]}{$$row[0]}++; } } print Dumper %map;
using print Dumper - I get what I need - its printing it out w/o Dumper that Im having issues with

The structure Im looking for would look like:
LSS1
     LUN1
          HOST1
          HOST2
          HOST3
     LUN2
          HOST1
          HOST3
LSS2

Im quite stumped...

Thanks
UPDATE
Thanks guys... I ended up using the following to print it out
for my $LSS ( sort keys %map ) { print "LSS: $LSS\n"; for my $LUNS ( sort keys %{ $map{$LSS} } ) { print " LUN: $LUNS LSS: $LSS\n\n"; for my $hosts ( sort @{ $map{$LSS}{$LUNS} } ) { print " $hosts\n"; } print "\n"; } }
Ted
--
"That which we persist in doing becomes easier, not that the task itself has become easier, but that our ability to perform it has improved."
  --Ralph Waldo Emerson

Replies are listed 'Best First'.
Re: pulling data from a complex structure
by GrandFather (Saint) on Jan 23, 2006 at 21:04 UTC

    Using each for navigating hashes can clean things up a little:

    use strict; use warnings; my %netMap = ( 'LSS1' => { 'LUN1' => [qw(HOST1 HOST2 HOST3)], 'LUN2' => [qw(HOST1 HOST2)], }, 'LSS2' => { 'LUN3' => [qw(HOST5 HOST6)], 'LUN4' => [qw(HOST1)], }, ); while (my @entry = each %netMap) { print $entry[0] . "\n"; while (my @lun = each %{$entry[1]}) { print " $lun[0]\n"; print " $_\n" for @{$lun[1]}; } }

    Prints:

    LSS2 LUN4 HOST1 LUN3 HOST5 HOST6 LSS1 LUN2 HOST1 HOST2 LUN1 HOST1 HOST2 HOST3

    The down side is that you can't control the output order, it is dictated by the enumeration order returned by each.


    DWIM is Perl's answer to Gödel
Re: pulling data from a complex structure
by BrowserUk (Patriarch) on Jan 23, 2006 at 20:36 UTC

    Maybe this will help? I tend to use a little more punctuation that is strictly necessary when accessing nested structures, but I find easier to use it than remember when I can get away with not doing so. I think the consistancy helps readability also.

    If you don't like the -l switch, you'll have to append "\n" to all the print statements.

    #! perl -slw use strict; my $map = { LSS1 => { LUN1 => [ qw[ HOST1 HOST2 HOST3 ] ], LUN2 => [ qw[ HOST1 HOST3 ] ], }, LSS2 => { LUN1 => [ qw[ HOST1 HOST2 HOST3 ] ], LUN2 => [ qw[ HOST1 HOST3 ] ], }, }; for my $L1 ( keys %{ $map } ) { print $L1; for my $L2 ( keys %{ $map->{ $L1 } } ) { print "\t$L2"; print "\t\t$_" for @{ $map->{ $L1 }{ $L2 } }; } } __END__ P:\test>junk4 LSS2 LUN2 HOST1 HOST3 LUN1 HOST1 HOST2 HOST3 LSS1 LUN2 HOST1 HOST3 LUN1 HOST1 HOST2 HOST3

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: pulling data from a complex structure
by planetscape (Chancellor) on Jan 24, 2006 at 06:12 UTC