homepage Welcome to WebmasterWorld Guest from 54.237.95.6
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor
Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
Forum Library, Charter, Moderators: coopster & jatar k & phranque

Perl Server Side CGI Scripting Forum

    
Hashes Of Arrays.
Davo1977




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

Can someone show me some examples of deleting, iterating of Hashes of arrays. AND operations such as push, splice and foreach.
Thanks

 

janharders




msg:3802641
 4:39 pm on Dec 8, 2008 (gmt 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

phranque




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

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved