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

Hey all, I'm trying to cycle through a hash and manipulate the values. If I have this situation (not the prettiest coding but hopefully you follow what I'm doing):

my %Hash; $Hash{a}[0]="i"; $Hash{a}[1]="ii"; $Hash{a}[2]="iii"; $Hash{b}[0]="iv"; $Hash{b}[1]="v"; $Hash{b}[2]="vi"; foreach my $var (sort keys %Hash) { print "$var,$Hash{$var}[0],$Hash{$var}[1],$Hash{$var}[2],"; }

The output I get is: a,i,ii,iii,b,iv,v,vi,

So for each key ($var either a or b) I have three values assigned to elements 0, 1, and 2. Can I cycle through those elements with another foreach loop and manipulate them individually without explicitly calling it with $Hash{$var}[0]? Am I doing something stupid here or too complicated and there's something easier I could do? Is this even a clear enough picture for someone to answer? :P Thanks for whatever help you can provide.

Replies are listed 'Best First'.
Re: Multiple Hash values
by boftx (Deacon) on Mar 06, 2015 at 21:26 UTC

    The short answer is: yes

    use strict; use warnings; my %Hash = ( a => [ qw( i ii iii ) ], b => [ qw( iv v vi ) ], ); # one way to do it for my $var ( sort keys %Hash ) { my $array_ref = $Hash{$var}; for my $element ( @{$array_ref} ) { print "$element,"; } } exit;

    The foregoing (untested) code is pretty basic, but I often reach for simple things if I want to make what I'm doing crystal clear to others and even myself months down the road.

    You must always remember that the primary goal is to drain the swamp even when you are hip-deep in alligators.
Re: Multiple Hash values
by marinersk (Priest) on Mar 06, 2015 at 21:26 UTC

    Yes, you can. You even have the right approach in mind. All you need is a syntax nudge.

    Hashes of Arrays and Arrays of Hashes can look a little odd at first but are intensely powerful structures if you take the time to get to know them.

    #!/usr/bin/perl use strict; my %Hash; $Hash{a}[0]="i"; $Hash{a}[1]="ii"; $Hash{a}[2]="iii"; $Hash{b}[0]="iv"; $Hash{b}[1]="v"; $Hash{b}[2]="vi"; foreach my $var (sort keys %Hash) { print "$var,"; foreach my $val (@{$Hash{$var}}) { if (defined $val) { print "$val,"; } } } exit; __END__

Re: Multiple Hash values
by RichardK (Parson) on Mar 06, 2015 at 23:20 UTC

    The perl data structures cookbook perldsc has lots of useful information and guidance for building complex structures. It's well worth a read.

    You should have a local copy, try 'perldoc perldsc'

Re: Multiple Hash values
by CountZero (Bishop) on Mar 07, 2015 at 07:56 UTC
    Of course you can!

    But if you only want to show the content of the hash (not use or manipulate them) then Data::Dumper and its brethren will come in very handy.

    use Modern::Perl qw /2014/; use Data::Dumper ; my %Hash; $Hash{a}[0]="i"; $Hash{a}[1]="ii"; $Hash{a}[2]="iii"; $Hash{b}[0]="iv"; $Hash{b}[1]="v"; $Hash{b}[2]="vi"; say Dumper (\%Hash);
    Output:
    $VAR1 = { 'b' => [ 'iv', 'v', 'vi' ], 'a' => [ 'i', 'ii', 'iii' ] };

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
Re: Multiple Hash values
by karlgoethebier (Abbot) on Mar 07, 2015 at 16:06 UTC

    But if you not only want to show the content of the hash (but use or manipulate it) Data::Rmap might be handy:

    #!/usr/bin/env perl use strict; use warnings; use Data::Rmap; use feature qw(say); my %hash; $hash{a}[0] = "i"; $hash{a}[1] = "ii"; $hash{a}[2] = "iii"; $hash{b}[0] = "iv"; $hash{b}[1] = "v"; $hash{b}[2] = "vi"; rmap {say} %hash; __END__
    "...apply a block to a data structure"

    Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

Re: Multiple Hash values
by chuckawood (Initiate) on Mar 09, 2015 at 17:13 UTC

    Thank you all for the help!! This will work nicely!

    Cheers.