CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Trouble accessing Hash of Arrays

 



sujeet97
New User

Apr 1, 2013, 7:16 PM

Post #1 of 7 (521 views)
Trouble accessing Hash of Arrays Can't Post

I am facing some trouble accessing values from a hash of arrays. I am attaching a sample code (this is not part of the main code but it pretty much shows the problem)


Code
#!/usr/bin/perl 

%sampleHash = ();
@array1 = ('a','b','c');
@array2=("x","y","z","s");
$sampleHash{"hash1"} = @array1;
$sampleHash{"hash2"} = @array2;

print $sampleHash{"hash1"}[0];


In the above code, the print statement is blank. I want it to show the value - a.
Please help. Its urgent.


(This post was edited by sujeet97 on Apr 1, 2013, 7:17 PM)


Kenosis
User

Apr 1, 2013, 7:46 PM

Post #2 of 7 (513 views)
Re: [sujeet97] Trouble accessing Hash of Arrays [In reply to] Can't Post

Cross-posted and answered at PerlMonks.


(This post was edited by Kenosis on Apr 2, 2013, 2:18 PM)


Laurent_R
Enthusiast / Moderator

Apr 2, 2013, 10:32 AM

Post #3 of 7 (496 views)
Re: [Kenosis] Trouble accessing Hash of Arrays [In reply to] Can't Post

Try to use references to your arrays, something like this:


Code
$sampleHash{"hash1"} = \@array1; 
$sampleHash{"hash2"} = \@array2;


And don't forget these two mandatory lines at the top of your program:


Code
use strict; 
use warnings;



g4143
Novice

Apr 7, 2013, 2:27 PM

Post #4 of 7 (477 views)
Re: [sujeet97] Trouble accessing Hash of Arrays [In reply to] Can't Post

Your code breaches a topic in Perl that can be very confusing and Perl doesn't help by having a single syntactic solution. I would investigate a book like Intermediate Perl which dedicates several chapters to this topic.


Code
#!/usr/bin/perl 

use warnings;
use strict;

my %sampleHash = ();
my @array1 = ('a','b','c');
my @array2=("x","y","z","s");
$sampleHash{hash1} = \@array1;
$sampleHash{hash2} = \@array2;

foreach (sort(keys(%sampleHash)))
{
my $h = $sampleHash{$_};
foreach ( 0..(@{$sampleHash{$_}} - 1) )
{
print ${$h}[$_], "\n";
}
}

__END__


The posted code should be clear as mud. The funny thing is, I wrote the code and still find it esoteric.


Zhris
User

Apr 7, 2013, 5:18 PM

Post #5 of 7 (467 views)
Re: [g4143] Trouble accessing Hash of Arrays [In reply to] Can't Post

Hi g4142,

FYI,

You can determine the subscript of the last element of the array directly:


Code
(@{$sampleHash{$_}} - 1) 
$#{$sampleHash{$_}}


Also in your second foreach; 1) looping / accessing via subscript is unnecessary 2) you could have made use of previously assigned $h in the condition.

Here are my simplified versions:


Code
# without sorting: 
foreach my $list (values %sampleHash)
{
print "$_\n" foreach (@$list);
}


# without sorting while:
while (my ($unused_key, $list) = each (%sampleHash))
{
print "$_\n" foreach (@$list);
}


# with sorting:
foreach my $key (sort keys %sampleHash)
{
print "$_\n" foreach (@{$sampleHash{$key}});
}


In fact, if we want to make them more compact:


Code
$" = "\n"; # unlocalised list separator assignment 


# without sorting:
print "@$_\n" for (values %sampleHash);


# with sorting:
print "@$_\n" for (map { $sampleHash{$_} } sort keys %sampleHash);


Regards,

Chris


(This post was edited by Zhris on Apr 7, 2013, 6:12 PM)


g4143
Novice

Apr 7, 2013, 6:22 PM

Post #6 of 7 (435 views)
Re: [Zhris] Trouble accessing Hash of Arrays [In reply to] Can't Post


In Reply To
Hi g4142,

FYI,

You can determine the subscript of the last element of the array directly:


Code
(@{$sampleHash{$_}} - 1) 
$#{$sampleHash{$_}}


Also in your second foreach; 1) looping / accessing via subscript is unnecessary 2) you could have made use of previously assigned $h in the condition.

Here are my simplified versions:


Code
# without sorting: 
foreach my $list (values %sampleHash)
{
print "$_\n" foreach (@$list);
}


# without sorting while:
while (my ($unused_key, $list) = each (%sampleHash))
{
print "$_\n" foreach (@$list);
}


# with sorting:
foreach my $key (sort keys %sampleHash)
{
print "$_\n" foreach (@{$sampleHash{$key}});
}


In fact, if we want to make them more compact:


Code
$" = "\n"; # unlocalised list separator assignment 


# without sorting:
print "@$_\n" for (values %sampleHash);


# with sorting:
print "@$_\n" for (map { $sampleHash{$_} } sort keys %sampleHash);


Regards,

Chris


Thanks for the tips Chris. I'm new to Perl and I'm just getting the basics down.


BillKSmith
Veteran

Apr 7, 2013, 7:34 PM

Post #7 of 7 (428 views)
Re: [sujeet97] Trouble accessing Hash of Arrays [In reply to] Can't Post

Had you used strict and warnings, you would have received an error message rather than unexpected behavior. This is a perfect example of the reason we encourage you to always use them.


Code
#!/usr/bin/perl 
use strict;
use warnings;
my %sampleHash = ();
my @array1 = ( 'a', 'b', 'c' );
my @array2 = ( "x", "y", "z", "s" );
$sampleHash{"hash1"} = @array1;
$sampleHash{"hash2"} = @array2;
print $sampleHash{"hash1"}[0];


Result:

Code
Can't use string ("3") as an ARRAY ref while "strict refs" in use at C:\........

Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives