?
Solved

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

Posted on 2003-04-01
8
Medium Priority
?
249 Views
Last Modified: 2010-04-17
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']
);


0
Comment
Question by:mmedwid
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 1000 total points
ID: 8251527

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

Expert Comment

by:ozo
ID: 8251554
#or
use Data::Dumper;
print Dumper(\%hashTable);
0
 
LVL 1

Author Comment

by:mmedwid
ID: 8254692
Awesome!  So terse...so beautiful!!  You rock.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Author Comment

by:mmedwid
ID: 8254904
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.  
0
 
LVL 1

Author Comment

by:mmedwid
ID: 8254974
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.  
0
 
LVL 84

Expert Comment

by:ozo
ID: 8255811
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?
0
 
LVL 1

Author Comment

by:mmedwid
ID: 8256175
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.
0
 
LVL 84

Expert Comment

by:ozo
ID: 8256850
#pass a ref to %hashTable to Dumper:
use Data::Dumper;
$Data::Dumper::Indent=1;
print Dumper(\%hashTable);
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about problems I had with the new location API and permissions in Marshmallow
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
Six Sigma Control Plans
Starting up a Project
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question