Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > FAQ 4.55 How do I process an entire hash?

Reply
Thread Tools

FAQ 4.55 How do I process an entire hash?

 
 
PerlFAQ Server
Guest
Posts: n/a
 
      04-07-2011
This is an excerpt from the latest version perlfaq4.pod, which
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .

--------------------------------------------------------------------

4.55: How do I process an entire hash?

(contributed by brian d foy)

There are a couple of ways that you can process an entire hash. You can
get a list of keys, then go through each key, or grab a one key-value
pair at a time.

To go through all of the keys, use the "keys" function. This extracts
all of the keys of the hash and gives them back to you as a list. You
can then get the value through the particular key you're processing:

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

Once you have the list of keys, you can process that list before you
process the hash elements. For instance, you can sort the keys so you
can process them in lexical order:

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

Or, you might want to only process some of the items. If you only want
to deal with the keys that start with "text:", you can select just those
using "grep":

foreach my $key ( grep /^text:/, keys %hash ) {
my $value = $hash{$key}
...
}

If the hash is very large, you might not want to create a long list of
keys. To save some memory, you can grab one key-value pair at a time
using "each()", which returns a pair you haven't seen yet:

while( my( $key, $value ) = each( %hash ) ) {
...
}

The "each" operator returns the pairs in apparently random order, so if
ordering matters to you, you'll have to stick with the "keys" method.

The "each()" operator can be a bit tricky though. You can't add or
delete keys of the hash while you're using it without possibly skipping
or re-processing some pairs after Perl internally rehashes all of the
elements. Additionally, a hash has only one iterator, so if you use
"keys", "values", or "each" on the same hash, you can reset the iterator
and mess up your processing. See the "each" entry in perlfunc for more
details.



--------------------------------------------------------------------

The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.

If you'd like to help maintain the perlfaq, see the details in
perlfaq.pod.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Beginning .Net : Select entire month or entire week using selectionmode in calendar control with C# Examples and VB.Net Examples jayeshsorathia@gmail.com ASP .Net 0 09-26-2012 07:17 AM
FAQ 5.29 How can I read in an entire file all at once? PerlFAQ Server Perl Misc 0 03-16-2011 04:00 AM
FAQ 5.43 How do I copy an entire directory? PerlFAQ Server Perl Misc 0 02-27-2011 05:00 AM
FAQ 5.29 How can I read in an entire file all at once? PerlFAQ Server Perl Misc 0 01-26-2011 05:00 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57