KodFun
0
Q:

perl add key value pair to hash

#!/usr/bin/perl

#-----------------------------------------
# perl-hash-add.pl
# created by alvin alexander, devdaily.com
#-----------------------------------------

# create a perl hash

$prices{'pizza'} = 12.00;
$prices{'coke'} = 1.25;
$prices{'sandwich'} = 3.00;

# traverse the perl hash and print
# the key/value pairs

print "\nforeach output:\n";
foreach $key (keys %prices)
{
  # do whatever you want with $key and $value here ...
  $value = $prices{$key};
  print "  $key costs $value\n";
}
0

New to Communities?

Join the community