in reply to sorting hash alphabetically with numbering

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.

Replies are listed 'Best First'.
Re^2: sorting hash alphabetically with numbering
by reebee3 (Novice) on Oct 12, 2015 at 03:47 UTC
    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