Hashes in Perl (part 1)

Define a hash (associative array):

my %hash = ("key" => "value", currentlyPlaying => "Trans Am",
  "is_$someScalar" => 1);

Determine the number of elements in a hash (works on arrays too), similar to a .length() or UBound - use the scalar function:

my $hashLength = scalar %hash;
my $arrLength = scalar(@array); # this would work too

To quickly print out the contents of a hash, use keys:

print "Content-type:text/html\n\n";
foreach $k (keys %vars) { print "$k => ".$vars{$k}; }

That's it for now.

Posted 02 Aug 2007, tagged with perl