Saturday, May 31, 2014

Arrange input lines in alphabetical order

Script

#!/usr/bin/perl

print ("Print the names of a few animals:\n");
print ("Press Ctrl D when done: \n");
@input = <STDIN>;
@input = sort (@input);
print ("\n\nThe lines arranged in alphabetical order would be: \n");
print (@input);
print ("\n\n");


Execution

Print the names of a few animals:
Press Ctrl D when done:
crocodile
giraffe
fox
alligator
cat
turtle
wolf
ant
dog
rabbit
tiger
elephant
tortoise
fish
spider
cockroach
lion
frog
leopard
cheetah


The lines arranged in alphabetical order would be:
alligator
ant
cat
cheetah
cockroach
crocodile
dog
elephant
fish
fox
frog
giraffe
leopard
lion
rabbit
spider
tiger
tortoise
turtle
wolf

No comments:

Post a Comment