reebee3 has asked for the wisdom of the Perl Monks concerning the following question:
Hello! This is the output I am trying to achieve...
$VAR1 = { 'Tucson' => 'AZ', 'Boston' => 'MA', 'Jackson' => 'MS', 'Dixon' => 'NM', 'Denton' => 'TX', 'Cincinnati' => 'OH' }; 1: Boston, MA 2: Cincinnati, OH 3: Denton, TX 4: Dixon, NM 5: Jackson, MS 6: Tucson, AZ
I am having trouble getting the cities to sort alphabetically and am not sure how to incorporate the numbering on the alphabetical sort. The Dumper output is just fine.
This is what I have so far...
#!/usr/bin/env perl use strict; use warnings; use autodie; use Data::Dumper; my %cities = ( Tucson => 'AZ', Boston => 'MA', Jackson => 'MS', Dixon => 'NM', Denton => 'TX', Cincinnati => 'OH', ); print Dumper(\%cities); foreach my $place (sort { $cities{$a} cmp $cities{$b} } keys %cities) +{ my $state = $cities{$place} ; print "$place, $state \n"; }
Thank you
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: sorting hash alphabetically with numbering
by BrowserUk (Patriarch) on Oct 12, 2015 at 03:01 UTC | |
by reebee3 (Novice) on Oct 12, 2015 at 03:47 UTC | |
by kcott (Archbishop) on Oct 12, 2015 at 04:07 UTC | |
|
Re: sorting hash alphabetically with numbering
by AnomalousMonk (Archbishop) on Oct 12, 2015 at 03:05 UTC |