Hi,

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

@age = sort sortAge(@age);

and the expected result is like
@age = ( @f4=(jason, 71), @f2=(bill, 55), @f1=(amy, 35), @f3=(george, +28))
but when i put the subroutine in a module, it doesn't get sorted.

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

my @array = caller; ${$"{array}::b"}->[1] <=> ${$"{array}::a"}->[1]
but I cannot understand the syntax at all.</code>

So....please help!!! Thanks in advance!


In reply to Using a sorting subroutine in a module with an array of arrays as parameters by polmed

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.