DEVFYI - Developer Resource - FYI

How do I do < fill-in-the-blank > for each element in a hash?

Perl Questions and Answers


(Continued from previous question...)

How do I do < fill-in-the-blank > for each element in a hash?

Here's a simple technique to process each element in a hash:

#!/usr/bin/perl -w

%days = (
        'Sun' =>'Sunday',
        'Mon' => 'Monday',
        'Tue' => 'Tuesday',
        'Wed' => 'Wednesday',
        'Thu' => 'Thursday',
        'Fri' => 'Friday',
        'Sat' => 'Saturday' );

foreach $key (sort keys %days) {
  print "The long name for $key is $days{$key}.\n";
}

(Continued on next question...)

Other Interview Questions