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']
);
e.g.
my %hashTable = (
10 => ['Michael', ' Smith', '1961'],
20 => ['Sonya', 'Smith', '1969'],
30 => ['Alex', 'Smith', '1997']
);
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Awesome! So terse...so beautiful!! You rock.
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/m medwid/scr ipts]: 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.
"Not an ARRAY reference at test2e line 13."
[87]netmon2[/export/home/m
#!/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.
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/m medwid/scr ipts]: 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.
"Not an ARRAY reference at test2e line 13."
[87]netmon2[/export/home/m
#!/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?
'6/9' => {
'vlan' => {
128 => {
'mac' => {
'00-b0-d0-62-83-ba' => 1
}
}
}
}
};
Did you want it to look like something else?
ASKER
When I ran it on my more simple multivariable hash - the output came out exacly as I hoped...
[43]netmon2[/export/home/m medwid/scr ipts]: 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.
[43]netmon2[/export/home/m
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);
use Data::Dumper;
$Data::Dumper::Indent=1;
print Dumper(\%hashTable);
use Data::Dumper;
print Dumper(\%hashTable);