polmed has asked for the wisdom of the Perl Monks concerning the following question:
Basically I'm trying to use a sorting function from a module as i want to sort an array of arrays. I've stuck on this step for about 3 hrs, and just can't get the array of arrays to sorted as I expected.
personel.pm: package personel; use Data::Dumper; sub sortAge { $b->[1] <=> $a->[1]; } return 1;
in another file,
#under test.pl @age = ( @f1 = (amy,35), @f2=(bill,55), @f3=(george,28), @f4=(jason,71 +)); @age = sort personal::sortAge(\@age);
In a stand-alone program where subroutine is together with @age, I can simply do
and the expected result is like@age = sort sortAge(@age);
but when i put the subroutine in a module, it doesn't get sorted.@age = ( @f4=(jason, 71), @f2=(bill, 55), @f1=(amy, 35), @f3=(george, +28))
I also tried
personel::sortAge(@age)
as passing directly without a reference, but the Dumper print only 1 value, as if the @age didn't get pass in successfully. In order to pass an array as a parameter to a subroutine of a module, pass by reference is needed. So I'm really confused now as how to get the @age to sort properly.I read online that you can do something like
but I cannot understand the syntax at all.</code>my @array = caller; ${$"{array}::b"}->[1] <=> ${$"{array}::a"}->[1]
So....please help!!! Thanks in advance!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Using a sorting subroutine in a module with an array of arrays as parameters
by LanX (Saint) on Oct 18, 2013 at 22:40 UTC | |
|
Re: Using a sorting subroutine in a module with an array of arrays as parameters
by AnomalousMonk (Archbishop) on Oct 19, 2013 at 20:18 UTC | |
by LanX (Saint) on Oct 19, 2013 at 20:25 UTC |