Perl Programming Question:
Download Job Interview Questions and Answers PDF
How do I do < fill-in-the-blank > for each element in a hash?
Answer:
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";
}
#!/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";
}
Download Perl Programming Interview Questions And Answers
PDF
Previous Question | Next Question |
What does Perl do if you try to exploit the execve(2) race involving setuid scripts? | How do I sort a hash by the hash key? |