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