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


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



© 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