Welcome to WebmasterWorld Guest from 54.224.230.193

Forum Moderators: coopster & jatar k & phranque

Message Too Old, No Replies

Hashes Of Arrays.

     
3:46 pm on Dec 8, 2008 (gmt 0)

New User

5+ Year Member

joined:June 25, 2008
posts: 24
votes: 0


Can someone show me some examples of deleting, iterating of Hashes of arrays. AND operations such as push, splice and foreach.
Thanks
4:39 pm on Dec 8, 2008 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:May 31, 2008
posts:661
votes: 0


in general, an array-ref is just your usual scalar, e.g. you get it with

$hash{'mykey'}

now, if you want to operate on that array, you have to dereference it. I find -> the easiest way, e.g. to get the first element of that arrayref:

$hash{'mykey'}->[0]

now, to use the array-functions, you need list-context, e.g.
push @{ $hash{'mykey'} }, 'new element';
or
print scalar @{ $hash{'mykey'} };

iteration is simple:


my %hash = ('ar1' => ['elem1', 'elem2'], 'ar2' => ['elem3', 'elem4']);


for my $key (keys %hash)
{
print "now running: " . $key . "\n";
for (my $i = 0; $i < scalar @{ $hash{$key} }; $i++)
{
print "\t" . $i . ": " . $hash{$key}->[$i] . "\n";
}
}

pasting code for anonymous array refs actually breaks the ubb-code, so I had to split it into two blocks.

I haven't tested that code, but it should work fine. If not, report back and I'll try to help

12:42 am on Dec 9, 2008 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:10553
votes: 13


here is some information from the perl man page on the subject:
[perldoc.perl.org...]