Beast <mailto:[EMAIL PROTECTED]> wrote:
> I have an array:
>
>
> my @employee = ( [29243, 'john', 'John doe'],
> [24322, 'jane', 'Jane doe'],
> [27282, 'james', 'James doe']
> );
>
>
> Is there any builtin function in perl to sort the above array based
> on uid, username or fulname?
perldoc -f sort.
Something like this should work.
__BEGIN__
use strict;
use warnings;
my @employee = ( [29243, 'john', 'John doe'],
[24322, 'jane', 'Jane doe'],
[27282, 'james', 'James doe']
);
# Specify the column on what you want to search as a argument..
my $i = $ARGV[0] || 0;
# sort subroutine..
my $subsort = sub {
#no warnings;
$a->[$i] <=> $b->[$i]
||
$a->[$i] cmp $b->[$i]
};
print "Sorting by column $i\n";
foreach my $j ( sort $subsort @employee ){
print "@$j\n";
}
__END__
Some sample runs gave me the following result...
$-> test-perl.pl 0
Sorting by column 0
24322 jane Jane doe
27282 james James doe
29243 john John doe
$-> test-perl.pl 1
Sorting by column 1
27282 james James doe
24322 jane Jane doe
29243 john John doe
$-> test-perl.pl 2
Sorting by column 2
27282 james James doe
24322 jane Jane doe
29243 john John doe
There can be a better way to do it...
HTH....
Note: Will give warnings if warnings is enabled for columns which have
text.
--Ankur
Bad style destroys an otherwise superb program.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>