sorting an array of hashrefs

Discussion in 'Perl Misc' started by John W. Krahn, Aug 29, 2006.

  1. monkeys paw wrote:
    > The following is intended to sort an array of hash refs based on
    > id_type. It is not working however. Does anyone know why?
    >
    > use Data::Dumper;
    >
    > our %datatype_sort = (
    > bill => 0,
    > reg => 10,
    > );
    >
    >
    > my @ary = (
    > {id_type => 'reg', id => 'KY200616813'},
    > {id_type => 'bill', id => 'CA2005000A5'},
    > {id_type => 'bill', id => 'CA2005000A250'},
    > );
    >
    > my @x = sort _sortval_datatype @ary;
    >
    > print Dumper(\@x);
    >
    > sub _sortval_datatype {
    > my($av, $bv) = @_;
    > return $datatype_sort{$av->{id_type}} <=>
    > $datatype_sort{$bv->{id_type}};
    > }



    sub _sortval_datatype {
    return $datatype_sort{$a->{id_type}} <=> $datatype_sort{$b->{id_type}};
    }



    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Aug 29, 2006
    #1
    1. Advertising

  2. John W. Krahn

    monkeys paw Guest

    The following is intended to sort an array of hash refs based on
    id_type. It is not working however. Does anyone know why?

    use Data::Dumper;

    our %datatype_sort = (
    bill => 0,
    reg => 10,
    );


    my @ary = (
    {id_type => 'reg', id => 'KY200616813'},
    {id_type => 'bill', id => 'CA2005000A5'},
    {id_type => 'bill', id => 'CA2005000A250'},
    );

    my @x = sort _sortval_datatype @ary;

    print Dumper(\@x);

    sub _sortval_datatype {
    my($av, $bv) = @_;
    return $datatype_sort{$av->{id_type}} <=>
    $datatype_sort{$bv->{id_type}};
    }
     
    monkeys paw, Aug 29, 2006
    #2
    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:
    397
    markspace
    Jun 25, 2009
  2. Roedy Green
    Replies:
    1
    Views:
    449
    Roedy Green
    Jun 25, 2009
  3. monkeys paw

    array of hashrefs (nested)

    monkeys paw, Oct 27, 2003, in forum: Perl Misc
    Replies:
    9
    Views:
    175
    Bob Walton
    Oct 28, 2003
  4. Matija Papec

    grouping hashrefs to trees

    Matija Papec, Nov 16, 2003, in forum: Perl Misc
    Replies:
    1
    Views:
    78
    Ben Morrow
    Nov 16, 2003
  5. Sam
    Replies:
    6
    Views:
    175
    Anno Siegel
    Jun 15, 2005
Loading...

Share This Page