Data Dumper guidance

Discussion in 'Perl Misc' started by Kasp, Nov 13, 2003.

  1. Kasp

    Kasp Guest

    Hi Everyone,

    Need some help in Data::Dumper.
    What I want to do is sort this array (@arr) based on the first element of
    the sub-arrays.
    So I want finally is @sarr = ([3,2], [5,0], [6,3]);

    Here is the code:
    use Data::Dumper;
    my @arr = ([5,0], [3,2], [6,3]);
    print Data::Dumper->Dump(\@arr);
    my @sarr = sort { $a <=> $b} @arr;
    print Data::Dumper->Dump(\@sarr);

    TIA
    --
     
    Kasp, Nov 13, 2003
    #1
    1. Advertising

  2. In article <bova38$1j5p2l$-berlin.de>, Kasp wrote:
    > Hi Everyone,
    >
    > Need some help in Data::Dumper.
    > What I want to do is sort this array (@arr) based on the first element of
    > the sub-arrays.
    > So I want finally is @sarr = ([3,2], [5,0], [6,3]);



    So, really this has nothing to do with Data::Dumper, but with
    sorting.


    > Here is the code:
    > use Data::Dumper;
    > my @arr = ([5,0], [3,2], [6,3]);
    > print Data::Dumper->Dump(\@arr);
    > my @sarr = sort { $a <=> $b} @arr;


    That will sort on the array references in @arr.

    You want this:
    my @sarr = sort { $a->[0] <=> $b->[0]} @arr;



    --
    Andreas Kähäri
     
    Andreas Kahari, Nov 13, 2003
    #2
    1. Advertising

  3. Kasp

    Kasp Guest

    > You want this:
    > my @sarr = sort { $a->[0] <=> $b->[0]} @arr;


    Thanks Andreas.

    --
     
    Kasp, Nov 13, 2003
    #3
    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. kamal
    Replies:
    0
    Views:
    989
    kamal
    Aug 12, 2003
  2. Eric
    Replies:
    0
    Views:
    4,434
  3. rc
    Replies:
    3
    Views:
    1,881
    Chris Uppal
    Aug 16, 2004
  4. Sam

    Data::Dumper for Java?

    Sam, Nov 30, 2004, in forum: Java
    Replies:
    4
    Views:
    7,710
    Anonymous
    Feb 11, 2011
  5. Replies:
    4
    Views:
    250
    Paul Lalli
    Aug 1, 2007
Loading...

Share This Page