To sort an array in ascending order in Perl, you can use the `sort` function for both arrays of integers and arrays of strings.
In this example,
@numbers
.sort
function to sort the array of integers in ascending order.@numbers = (5, 2, 8, 1, 9);
@sorted_numbers = sort {$a <=> $b} @numbers;
print join(' ', @sorted_numbers);
1 2 5 8 9
In this example,
@words
.sort
function to sort the array of strings in ascending order.@words = ("apple", "banana", "cherry", "date");
@sorted_words = sort @words;
print join(' ', @sorted_words);
apple banana cherry date
In this tutorial, we learned How to Sort an Array in Ascending Order in Perl language with well detailed examples.