Sorting Arrays
The
sort() function sorts each element of an array according to ASCII
Numeric standards. This function has following syntax:
sort [ SUBROUTINE ] LIST
This function sorts the LIST and returns the sorted array value. If
SUBROUTINE is specified then specified logic inside the SUBTROUTINE is applied
while sorting the elements.
#! /usr/bin/perl
# define an array
@foods = qw(pizza steak chicken burgers);
print "Before: @foods\n";
# sort this array
@foods = sort(@foods);
print "After: @foods\n";
This will produce following result:
Before: pizza steak chicken burgers
After: burgers chicken pizza steak
Please note that sorting is performed based on ASCII Numeric value of the
words. So the best option is to first transform every element of the array into
lowercase letters and then perform the sort function.
No comments:
Post a Comment