In this post i will explain the functions that can be used with array in Perl.
sort():
This function is used to sort the elements of an array. This function is specially used to sort elements of type string.
sort():
This function is used to sort the elements of an array. This function is specially used to sort elements of type string.
#! /Perl/bin/perl
print("Enter elements(only 0-9) of array to be sorted\n Enter Ctrl+Z to quit on windows\n");
@array=<STDIN>;
print("The elements of array before sorting are ");
print("@array\n");
@array=sort(@array);
print("The elements of sorted array are ");
print("@array");
Output:print("Enter elements(only 0-9) of array to be sorted\n Enter Ctrl+Z to quit on windows\n");
@array=<STDIN>;
print("The elements of array before sorting are ");
print("@array\n");
@array=sort(@array);
print("The elements of sorted array are ");
print("@array");
We can also use this method to sort integers but their range should be between 0-9. Since this method will compare the first character of elements first and after that if they are equal then it check for second character. It compares in this way because this method is used to sort strings. I have supplied input between 0-9 and the output is given below
But if we will give input beyond this range then the output will not be correct.
See below output screen
0 comments:
Post a Comment