Modify the Jetson script jet_hash.pl so that the values and the corresponding ke
ID: 651197 • Letter: M
Question
Modify the Jetson script jet_hash.pl so that the values and the corresponding key is output in a numerically sorted order. (You can not use any "special" operators)
#!/usr/bin/perl
use 5.012;
my %jetsons = (
7 => 'Judy',
10 => 'Jane',
11 => 'George',
2 => 'Elroy',
100=> 'Rosey',
1 => 'Astro',
19 => 'Mr. Spacely',
22 => 'Mr. Cogswell');
my @jetnums = keys (%jetsons);
print " unsorted list = @jetnums ";
my @sorted_jets = sort(@jetnums);
print " sorted list = @sorted_jets ";
foreach (@sorted_jets) {
printf "%3d %-15s ", $_, $jetsons{$_};
}
Explanation / Answer
#!/usr/bin/perl
use 5.012;
my %jetsons = (
7 => 'Judy',
10 => 'Jane',
11 => 'George',
2 => 'Elroy',
100=> 'Rosey',
1 => 'Astro',
19 => 'Mr. Spacely',
22 => 'Mr. Cogswell');
my @jetnums = keys (%jetsons);
print " unsorted list = @jetnums ";
my @sorted_jets = sort {$a <=> $b} @jetnums;
print " sorted list = @sorted_jets ";
foreach (@sorted_jets) {
printf "%3d %-15s ", $_, $jetsons{$_};
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.