Looping through Perl data types

hash

http://stackoverflow.com/questions/3033/whats-the-safest-way-to-iterate-through-the-keys-of-a-perl-hash

% perldoc -f keys
% perldoc -f each

If you just want the keys and do not plan to ever read any of the values, use keys():

foreach my $key (keys %hash) { ... }

If you just want the values, use values():

foreach my $val (values %hash) { ... }

If you need the keys and the values, use each():

keys %hash; # reset the internal iterator so a prior each() doesn't affect the loop
while(my($k, $v) = each %hash) { ... }

#programming - #perl

From JR's : articles
89 words - 557 chars
created on - #
source - versions

Related articles
Perl regex extracting domain name from URL - Oct 02, 2013
Perl Programming Environments - May 06, 2013
Perl programming tools to test - Nov 14, 2014
Probably my favorite Web apps to use and create - Jan 15, 2014
Perl try, catch, eval, die, warn, carp - Jun 25, 2013
more >>



A     A     A     A     A

© 2013-2017 JotHut - Online notebook

current date: May 1, 2024 - 11:52 a.m. EDT