Nymn has asked for the wisdom of the Perl Monks concerning the following question:

I need to sort a list of names in alphabetical order, but can't figure out how I'd get to do that. Here's the program:
#!/usr/bin/perl # @names = qw { steve Mary alice Robert MARVIN kENneth tiM } ; # foreach $name (@names) { print "Name = $name\n" ; } ; # print "\nSorted names 1 \n" ; # New code to sort and print names goes here.
I know to put in
sort @names foreach $name (@names) { print "Name: $name \n" ; } ;
Is there a way to get the names in order? I tried the codes, but all I got was:
Name = MARVIN
Name = Mary
Name = Robert
Name = alice
Name = kENneth
Name = steve
Name = tiM
There's no way to get it to sort them so alice is first, then kEN, and so on?

Replies are listed 'Best First'.
Re: Sorting names
by swampyankee (Parson) on Dec 20, 2006 at 18:08 UTC

    The Perl function for sorting is named "sort." You may also want to browse the Categorized Q&A "QandASection: sorting."

    Note that sort is sensitive to case, as it uses the ASCII collating sequence.

    emc

    At that time [1909] the chief engineer was almost always the chief test pilot as well. That had the fortunate result of eliminating poor engineering early in aviation.

    —Igor Sikorsky, reported in AOPA Pilot magazine February 2003.
Re: Sorting names
by jettero (Monsignor) on Dec 20, 2006 at 18:03 UTC

    My bets are that you just want a simple @sortednames = sort { lc($a) cmp lc($b) } @names. If that syntax is unclear, check the sort docs for further info.

    -Paul

Re: Sorting names
by leighsharpe (Monk) on Dec 20, 2006 at 23:15 UTC
    What about:
    print join("\n",sort(@names))."\n";