I am getting an error while trying to sort a simple array…
The ERROR reads: “use of uninitialized value in numeric comparison (<=>) at file.pl line #”
#!/usr/bin/perl
use strict
use wardings
use Data::Dumper
my @array
my $array
$array[1]= 5
$array[2]= 2
$array[3]= 3
$array[4]= 4
$array[5]= 1
sub numerically {$a <=> $b}
my @sortedarray = sort numerically @array;
print "@sortedarray\n";
I am just trying to sort the array to get:
1 2 3 4 5
I am new at perl so this might just be something stupid, but please help me… Thanks
Arrays are indexed starting at 0. The error comes from trying to sort the array when
$array[0]is undefined.Update: Also, in perl, one would write:
There is no point in declaring
$array— that would be a scalar. You are only working with the array@array, even though it is called with a$. Please read the perl documentation.