homepage Welcome to WebmasterWorld Guest from 54.205.122.62
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
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

5+ Year Member



 
Msg#: 3802611 posted 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

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 3802611 posted 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

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



 
Msg#: 3802611 posted 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.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved