Link to home
Start Free TrialLog in
Avatar of mmedwid
mmedwid

asked on

Terse Means of Printing All Values From Hash with Multiple Values?

As in the example below - I would like to print out the key and all the values in a multi value hash table.  What is the tersest way to accompish this?  Thanks.


e.g.

my %hashTable = (
        10 => ['Michael', ' Smith', '1961'],
        20 => ['Sonya', 'Smith', '1969'],
        30 => ['Alex', 'Smith', '1997']
);


ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
#or
use Data::Dumper;
print Dumper(\%hashTable);
Avatar of mmedwid
mmedwid

ASKER

Awesome!  So terse...so beautiful!!  You rock.
Avatar of mmedwid

ASKER

Say - when I tried to use this in another instance I got a
"Not an ARRAY reference at test2e line 13."

[87]netmon2[/export/home/mmedwid/scripts]: more test2e
#!/usr/bin/perl

use lib '/neteng/lib';
use catos;

my $host = "snv2-41-sw1.gumbo.com";
my $pw = 'chili';

my %camTable = catos::getCamTable($host, $pw);

for(keys %camTable){
   print "$_ [@{$camTable{$_}}]\n";
}

If I use data cumper on this hash I get output that looks like...

          '6/9' => {
                     'vlan' => {
                                 128 => {
                                          'mac' => {
                                                     '00-b0-d0-62-83-ba' => 1
                                                   }
                                        }
                               }
                   }
        };

If you want me to post another question for this - just let me know.  
Avatar of mmedwid

ASKER

Say - when I tried to use this in another instance I got a
"Not an ARRAY reference at test2e line 13."

[87]netmon2[/export/home/mmedwid/scripts]: more test2e
#!/usr/bin/perl

use lib '/neteng/lib';
use catos;

my $host = "snv2-41-sw1.gumbo.com";
my $pw = 'chili';

my %camTable = catos::getCamTable($host, $pw);

for(keys %camTable){
   print "$_ [@{$camTable{$_}}]\n";
}

If I use data cumper on this hash I get output that looks like...

          '6/9' => {
                     'vlan' => {
                                 128 => {
                                          'mac' => {
                                                     '00-b0-d0-62-83-ba' => 1
                                                   }
                                        }
                               }
                   }
        };

If you want me to post another question for this - just let me know.  
if I use data cumper on this hash I get output that looks like...

         '6/9' => {
                    'vlan' => {
                                128 => {
                                         'mac' => {
                                                    '00-b0-d0-62-83-ba' => 1
                                                  }
                                       }
                              }
                  }
       };
Did you want it to look like something else?
Avatar of mmedwid

ASKER

When I ran it on my more simple multivariable hash - the output came out exacly as I hoped...

[43]netmon2[/export/home/mmedwid/scripts]: test13b
10 [Michael  Smith 1961]
20 [Sonya Smith 1969]
30 [Alex Smith 1997]

...that was using your...

for( keys %hashTable ){
   print "$_ [@{$hashTable{$_}}]\n";
}

But using that with this other hash yielded the error message "Not an ARRAY reference at test2e line 13." Using dumper on the simple hash gives...

$VAR1 = 10;
$VAR2 = [
          'Michael',
          'Smith',
          1961
        ];
$VAR3 = 20;
$VAR4 = [
          'Sonya',
          'Smith',
          1969
        ];
$VAR5 = 30;
$VAR6 = [
          'Alex',
          'Smith',
          1997
        ];

I suspect the issue that the other hash is nested in a way I don't understand yet.  The whole point of all of this is I am trying to better understand how hash tables work and how to work with them.  

Thanks.
#pass a ref to %hashTable to Dumper:
use Data::Dumper;
$Data::Dumper::Indent=1;
print Dumper(\%hashTable);