Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2003-04-01
8
Medium Priority
?
267 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
  • 4
  • 4
8 Comments
 
LVL 85

Accepted Solution

by:
ozo earned 1000 total points
ID: 8251527

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

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 85

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 85

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
Progress
Starting up a Project

571 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