Sorting an array

Discussion in 'Perl Misc' started by gamo, Dec 18, 2009.

  1. gamo

    gamo Guest

    This code

    @sorted = sort {
    field4($a) <=> field4($b) ||
    field1($a) <=> field1($b)
    } @oferta;

    simply don't work.

    The error message is

    Undefined subroutine &main::field4 called at subasta.pl line 30.

    Any help is appreciated.


    --
    http://www.telecable.es/personales/gamo/
    Honesta turpitudo est pro causa bona --Publilius Syrus
    "Was it a car or a cat I saw?"
    perl -E 'say 111_111_111**2;'
     
    gamo, Dec 18, 2009
    #1
    1. Advertising

  2. gamo <> wrote:
    >This code
    >
    >@sorted = sort {
    > field4($a) <=> field4($b) ||
    > field1($a) <=> field1($b)
    >} @oferta;
    >
    >simply don't work.
    >
    >The error message is
    >Undefined subroutine &main::field4 called at subasta.pl line 30.


    Well, yeah, sorry for asking the obvious, but did you define a function
    field4() in your main program? You are using it in the comparision, but
    perl can't find it.

    jue
     
    Jürgen Exner, Dec 18, 2009
    #2
    1. Advertising

  3. gamo

    gamo Guest

    On Fri, 18 Dec 2009, Jürgen Exner wrote:

    > gamo <> wrote:
    > >This code
    > >
    > >@sorted = sort {
    > > field4($a) <=> field4($b) ||
    > > field1($a) <=> field1($b)
    > >} @oferta;
    > >
    > >simply don't work.
    > >
    > >The error message is
    > >Undefined subroutine &main::field4 called at subasta.pl line 30.

    >
    > Well, yeah, sorry for asking the obvious, but did you define a function
    > field4() in your main program? You are using it in the comparision, but
    > perl can't find it.
    >
    > jue
    >


    I'm afraid I take the FAQ suggestion too literally.
    How can be that subs be writed?

    The array @oferta has $oferta[0 to .5M][0 to 3]

    TIA

    --
    http://www.telecable.es/personales/gamo/
    Honesta turpitudo est pro causa bona --Publilius Syrus
    "Was it a car or a cat I saw?"
    perl -E 'say 111_111_111**2;'
     
    gamo, Dec 18, 2009
    #3
  4. gamo <> wrote:
    >On Fri, 18 Dec 2009, Jürgen Exner wrote:
    >
    >> gamo <> wrote:
    >> >This code
    >> >
    >> >@sorted = sort {
    >> > field4($a) <=> field4($b) ||
    >> > field1($a) <=> field1($b)
    >> >} @oferta;
    >> >
    >> >simply don't work.
    >> >
    >> >The error message is
    >> >Undefined subroutine &main::field4 called at subasta.pl line 30.

    >>
    >> Well, yeah, sorry for asking the obvious, but did you define a function
    >> field4() in your main program? You are using it in the comparision, but
    >> perl can't find it.

    >
    >I'm afraid I take the FAQ suggestion too literally.
    >How can be that subs be writed?
    >
    >The array @oferta has $oferta[0 to .5M][0 to 3]


    Insufficient information. Please post a minimal but complete program
    that we can compile and run. In your case what we really really need is
    an actual sample of your data structure (make sure your program creates
    that) as well as a clear, precise description of your sort criteria.

    jue
     
    Jürgen Exner, Dec 19, 2009
    #4
  5. gamo

    Jim Gibson Guest

    In article <>, gamo
    <> wrote:

    > On Fri, 18 Dec 2009, J¸rgen Exner wrote:
    >
    > > gamo <> wrote:
    > > >This code
    > > >
    > > >@sorted = sort {
    > > > field4($a) <=> field4($b) ||
    > > > field1($a) <=> field1($b)
    > > >} @oferta;
    > > >
    > > >simply don't work.
    > > >
    > > >The error message is
    > > >Undefined subroutine &main::field4 called at subasta.pl line 30.

    > >
    > > Well, yeah, sorry for asking the obvious, but did you define a function
    > > field4() in your main program? You are using it in the comparision, but
    > > perl can't find it.


    >
    > I'm afraid I take the FAQ suggestion too literally.
    > How can be that subs be writed?
    >
    > The array @oferta has $oferta[0 to .5M][0 to 3]


    If you mean that the @oferta array has 500_000 elements, each of which
    is a reference to an array with 4 elements, and you want to sort the
    @oferta array using the numerical value of the fourth element of the
    referenced arrays as a primary key and the numerical value of the first
    element as a secondary key, then what you want is (untested):

    @sorted = sort {
    $a->[3] <=> $b->[3] ||
    $a->[0] <=> $b->[0]
    } @oferta;

    --
    Jim Gibson
     
    Jim Gibson, Dec 19, 2009
    #5
  6. gamo

    gamo Guest

    On Fri, 18 Dec 2009, Jim Gibson wrote:

    > In article <>, gamo
    > <> wrote:
    >
    > > On Fri, 18 Dec 2009, J¸rgen Exner wrote:
    > >
    > > > gamo <> wrote:
    > > > >This code
    > > > >
    > > > >@sorted = sort {
    > > > > field4($a) <=> field4($b) ||
    > > > > field1($a) <=> field1($b)
    > > > >} @oferta;
    > > > >
    > > > >simply don't work.
    > > > >
    > > > >The error message is
    > > > >Undefined subroutine &main::field4 called at subasta.pl line 30.
    > > >
    > > > Well, yeah, sorry for asking the obvious, but did you define a function
    > > > field4() in your main program? You are using it in the comparision, but
    > > > perl can't find it.

    >
    > >
    > > I'm afraid I take the FAQ suggestion too literally.
    > > How can be that subs be writed?
    > >
    > > The array @oferta has $oferta[0 to .5M][0 to 3]

    >
    > If you mean that the @oferta array has 500_000 elements, each of which
    > is a reference to an array with 4 elements, and you want to sort the
    > @oferta array using the numerical value of the fourth element of the
    > referenced arrays as a primary key and the numerical value of the first
    > element as a secondary key, then what you want is (untested):
    >
    > @sorted = sort {
    > $a->[3] <=> $b->[3] ||
    > $a->[0] <=> $b->[0]
    > } @oferta;
    >


    This is exactly what I want. Thank you very much.


    > --
    > Jim Gibson
    >


    --
    http://www.telecable.es/personales/gamo/
    Honesta turpitudo est pro causa bona --Publilius Syrus
    "Was it a car or a cat I saw?"
    perl -E 'say 111_111_111**2;'
     
    gamo, Dec 19, 2009
    #6
  7. gamo

    ccc31807 Guest

    On Dec 18, 5:38 pm, gamo <> wrote:
    > This code
    >
    > @sorted = sort {
    >     field4($a) <=> field4($b) ||
    >       field1($a) <=> field1($b)
    >
    > } @oferta;
    >
    > simply don't work.


    Here is an example of working code (not sorting a hash but an array,
    but it shouldn't make any difference):

    foreach my $key (sort {
    $memberhash->{$a}{'membertype'} cmp $memberhash->{$b}
    {'membertype'} ||
    $memberhash->{$a}{'last'} cmp $memberhash->{$b}{'last'} ||
    $memberhash->{$a}{'first'} cmp $memberhash->{$b}{'first'} }
    keys %{$memberhash})

    This uses the block style rather than the function style. You might
    translate this into an array sort similar to this:

    foreach my $element (sort
    {
    #here is the specific sort criteria, like $a <=> $b
    }
    @my_array)
    {
    print "This is the element: $element\n";
    push @sorted, $element; #inverse sort
    }

    CC.
     
    ccc31807, Dec 19, 2009
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. markspace
    Replies:
    1
    Views:
    409
    markspace
    Jun 25, 2009
  2. Roedy Green
    Replies:
    1
    Views:
    454
    Roedy Green
    Jun 25, 2009
  3. Replies:
    2
    Views:
    1,491
    James Kanze
    Jul 6, 2010
  4. Dominic Son

    Sorting through an array of an array

    Dominic Son, Oct 13, 2006, in forum: Ruby
    Replies:
    5
    Views:
    90
  5. Andrew Poulos

    Sorting array against other array

    Andrew Poulos, Jan 17, 2007, in forum: Javascript
    Replies:
    4
    Views:
    110
    Andrew Poulos
    Jan 23, 2007
Loading...

Share This Page