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

    Like this?:

    #! perl -slw use strict; my %cities = ( Tucson => 'AZ', Boston => 'MA', Jackson => 'MS', Dixon => 'NM', Denton => 'TX', Cincinnati => 'OH', ); my @sorted = sort keys %cities; my $n = 0; printf "%d: %s, %s\n", ++$n, $_, $cities{ $_ } for @sorted; __END__ C:\test>1144458 1: Boston, MA 2: Cincinnati, OH 3: Denton, TX 4: Dixon, NM 5: Jackson, MS 6: Tucson, AZ

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Yes, exactly. Thank you!

      It works perfectly, but I don't quite follow this line...

      printf "%d: %s, %s\n", ++$n, $_, $cities{ $_ } for @sorted;

        printf takes a FORMAT: in this case, that's "%d: %s, %s\n". Those FORMATs are documented under the related function sprintf.

        The 'for @sorted' part is a statement modifier: documented under perlsyn: Statement Modifiers.

        Hopefully, the remaining parts are clear. If not, please specify what part(s) you're having difficulty with.

        — Ken

Re: sorting hash alphabetically with numbering
by AnomalousMonk (Archbishop) on Oct 12, 2015 at 03:05 UTC

    TMI — Too Much Information. You're actually sorting by the values of the keys (i.e., the state codes) and not by the keys (the cities).

    c:\@Work\Perl\monks>perl -wMstrict -MData::Dumper -le "my %cities = ( Tucson => 'AZ', Boston => 'MA', Jackson => 'MS', Dixon => 'NM', Denton => 'TX', Cincinnati => 'OH', ); print Dumper(\%cities); foreach my $place (sort keys %cities) { my $state = $cities{$place} ; print \"$place, $state \n\"; } " $VAR1 = { 'Cincinnati' => 'OH', 'Tucson' => 'AZ', 'Dixon' => 'NM', 'Jackson' => 'MS', 'Denton' => 'TX', 'Boston' => 'MA' }; Boston, MA Cincinnati, OH Denton, TX Dixon, NM Jackson, MS Tucson, AZ

    Update 1: I was going to add some example code to handle the numbering, but BrowserUk beat me to it.

    Update 2: This might give you a better idea of what's going on in your original sort block:

    c:\@Work\Perl\monks>perl -wMstrict -e "my %cities = qw( Tucson AZ Boston MA Jackson MS Dixon NM Denton TX Cincinnati O +H ); for my $place (sort { printf qq{'$cities{$a}' }; $cities{$a} cmp $c +ities{$b}; } keys %cities) { print qq{\n '$place' in '$cities{$place}'}; } " 'OH' 'NM' 'TX' 'AZ' 'MS' 'OH' 'AZ' 'MA' 'MS' 'TX' 'TX' 'Tucson' in 'AZ' 'Boston' in 'MA' 'Jackson' in 'MS' 'Dixon' in 'NM' 'Cincinnati' in 'OH' 'Denton' in 'TX'


    Give a man a fish:  <%-{-{-{-<