Bootstrap FreeKB - Perl (Scripting) - Sort array
Perl (Scripting) - Sort array

Updated:   |  Perl (Scripting) articles

Let's say you have an array of fruit. Notice the values are not in alphabetical order.

my @fruits = qw(banana apple grapes orange);

 

Dumper can be used to print the contents of the array.

use Data::Dumper;
print Dumper \@fruits;

 

Which should produce the following. The values are not sorted.

$VAR1 = [
          'banana',
          'apple',
          'grapes',
          'orange'
        ]

 

sort can be used to sort the array alphabetically.

@fruits = sort @fruits;

 

Using Dumper to print the array will now show that the array is sorted alphabetically.

VAR1 = [
          'apple',
          'banana',
          'grapes',
          'orange'
        ];

 


Reverse sort

reverse sort can be used to sort the array in reverse, alphabetically.

@fruits = reverse sort @fruits;

 

Using Dumper to print the array will now show that the array is reverse sorted alphabetically.

$VAR1 = [
          'orange',
          'grapes',
          'banana',
          'apple'
        ];

 


Case sensitive

In this example, there is a mix of upper and lower case values in the @fruits array.

my @fruits = qw(banana APPLE grapes ORANGE);

 

In this scenario, { "\L$a" cmp "\L$b" } is used to sort the array alphabetically.

@fruits = sort { "\L$a" cmp "\L$b" } @fruits;

 


Sort numbers / integers

Let's say you have an array of integers, like this.

my @numbers = qw(1 2 10);

 

And then you attempt to sort the array.

@numbers = sort @numbers;

 

Using Dumper will show that the array is not sorted numerically, since the sort function sorts alphabetically.

$VAR1 = [
          '1',
          '10',
          '2'
        ];

 

The following can be used to sort the array numerically, ascending.

@numbers = sort { $a cmp $b } @numbers;

 

The following can be used to sort the array numerically, descending.

@numbers = sort { $b cmp $a } @numbers;

 

Now Dumper will show that the array is sorted numerically.

$VAR1 = [
          '1',
          '2',
          '10'
        ];

 

Let's say @unsorted_array contains both integers and strings, like this.

$VAR1 = [
          '1,apple',
          '10,banana',
          '2,orange'
        ];

 

Here is how you would sort the array ascending on field 0, the integers.

my @sorted_array = sort { (split(',',$a))[0] <=> (split(',',$b))[0] } @unsorted_array;

 


Foreach loop

You can sort during a foreach loop, like this.

foreach my $fruit (sort @fruits) {
    print "$fruit\n";
}

 

Which should produce the following.

apple
banana
orange
grapes

 

However, this will not actually sort @fruits. Dumper would show that the array remains unsorted. Using sort during a foreach loop will only sort the output as part of the foreach loop.

$VAR1 = [
          'banana',
          'apple',
          'grapes',
          'orange'
        ]

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 62d3ca in the box below so that we can be sure you are a human.