How do write a PERL program that reads strings & sorts?

January 23, 2012 – 8:11 pm

The program needs to reads in an arbitrary number of strings from the command line and displays them sorted alphabetically.

Also, Provide a command line switch that controls the order of sorting (ascending [a-z], descending [z-a])

Related posts:

  1. Perl question?
  2. Perl. We were ask to do a program that reads a fasta sequence file and we were asked to clean, wrap…..?
  3. In perl programming what is a complete code that write a program that reads two integers from standard input?
  4. how to write a program in perl that reads a file and changes all low letters to capitals and capitals to small
  5. Can i run another program from a program i write in perl?
  6. Perl help: if condition to match multiple strings?
  7. How do you allow a Java program to accept an arbitrary number of text files as input when the program begins?
  8. How to return a number of random strings from a group of strings using PHP?
  9. How to a write a program in python to calculate pi?
  10. I have a doubt.How do I write a a message from my perl program to /var/log/messages ?
  1. One Response to “How do write a PERL program that reads strings & sorts?”

  2. !c:/perl/bin/perl.exe
    print "Enter your strings:\n";
    chomp(@list = <STDIN>);

    print "Unsorted strings:\n";
    foreach $_ (@list){
    printf "\n%20s", $_;
    }

    @sorted = sort @list;

    print "Sorted strings:\n";
    foreach $_ (@sorted){
    printf "\n%20s", $_;
    }

    print "\n\nDone!\n";

    …Produces
    DB<1> r
    Enter your strings:
    aaaaa
    ccccc
    bbbbb
    xxxxx
    mmmmm
    ^Z
    Unsorted strings:

    aaaaa
    ccccc
    bbbbb
    xxxxx
    mmmmmSorted strings:

    aaaaa
    bbbbb
    ccccc
    mmmmm
    xxxxx

    Done!

    HTH – I’ll leave it to you to do the switch thingy – just google "perl and $ARGV

    By kevin c on Jan 23, 2012

Sorry, comments for this entry are closed at this time.