Basic HTML version of Foils prepared Sept 20 97

Foil 33 Hashes -- Operators: keys, values, each

From Tutorial on PERL Computational Science for Information Age Course CPS616 -- Sept 20 97. by Geoffrey C. Fox,Nancy McCracken,Tom Scavo


1 keys(%dict) returns a list (array) of keys in %dict (in arbitrary order). This can be used with foreach construct:
2 foreach (keys(%mime)) { # $_ is default loop variable
  • print "\$mime{$_} = $mime{$_}\n";
3 }
4 values(%dict) is typically less useful. It returns an unordered list of values (with repetition) in hash %dict
5 each(%dict) returns a single, two-element list containing the "next" key-value pair in %dict. For example,
6 while ( ($key,$val) = each(%dict) ) { ... }
7 Every call to each(%dict) returns a new pair until it finally returns null. The next call to each() starts the cycle again...

in Table To:


© Northeast Parallel Architectures Center, Syracuse University, npac@npac.syr.edu

If you have any comments about this server, send e-mail to webmaster@npac.syr.edu.

Page produced by wwwfoil on Sun Sep 21 1997