Welcome to WebmasterWorld Guest from 54.162.240.235

Forum Moderators: coopster & jatar k & phranque

Message Too Old, No Replies

Hashes Of Arrays.

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

5+ Year Member



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)

WebmasterWorld Senior Member 5+ Year Member



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)

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



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

Featured Threads

Hot Threads This Week

Hot Threads This Month