Data Dumper guidance

K

Kasp

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
--
 
A

Andreas Kahari

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;
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top