Questions about this topic? Sign up to ask in the talk tab.

Perl/Basics/Hashes/Helper Functions

From NetSec
Revision as of 01:39, 19 July 2012 by Chantal21I (Talk | contribs) (Created page with "=====each()===== {{:Perl/Basics/Hashes/Helper Functions/Each}} =====keys===== {{:Perl/Basics/Hashes/Helper Functions/Keys}}")

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
each()

"while my each" can be used to isolate $key => $value pairs from a hash as follows with our %user hash:

<syntaxhighlight lang="perl">while(my($key,$value) = each(%user)) { print "Key: $key, Value: $value\n"; };</syntaxhighlight>

keys

This uses a foreach() loop and casting. We can isolate $key=>$value pairs the same as above using keys in stead of each:

<syntaxhighlight lang="perl">foreach my $key (@{sort keys %user}) { print "Key: $key, Value: ". $user{$key} ."\n"; };</syntaxhighlight>