Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to sort each list in the list of lists? 1

Status
Not open for further replies.

concino

Programmer
Jan 18, 2004
7
US
I'm trying to sort each list in the list of lists without using foreach and flattening the two dimentional array.
I already have a working code with foreach which does very complicated stuff, but I'd like to improve my code using map, so the only way it can be improved is to find a way to use sort without flattening the array.

right now, i can sort the inner arrays by their sizes, but i could not be able to figure out yet how to get inside of the arrays and sort them numerically.

thanks for your response.



 
You can use for(each) without flattening the list, and you don't need to use map. Here's an example that sorts the elements of each row in descending numerical order.

1. Loop over the rows
2. Dereference each row as an array, sort and store back into the origianl array

Code:
#!perl

use strict;
use Data::Dumper;

my @grades = (
	      [90, 16, 75, 52,],
	      [50, 37, 18, 65,],
	      [74, 90, 81, 50,],
	      [85, 47, 10, 23,],
	  );

for (@grades)
{
    @$_ = sort {$b <=> $a} @$_;
}

print Dumper(@grades);
 
yeah, but my co-worker is telling me that there is a much superp solution with map. I need that because i'm going to stick that sort to the behind of another map which does something else. So I should not use for(each) although I do have that working code which looks hairy.

so it is going to be:

@list = map { ... } sort { ... }

i think.
 
Sorry, I dunno. I'll be interested to see it when someone comes up with it. If you end up finding it someplace else, please post back and let us know what the solution is.
 
Thanks mikevh for your response.


I've never used sort inside of a map, and it took some time to figure out, so the solution should be like this:

map { @$_ = sort { $a <=> $b } @$_, $_ } @list;


 
Does that actually produce the result you wanted? I haven't actually seen any of your code of course, but this solution doesn't work if I try it with my example data.

Output from my code using for/example data:
$VAR1 = [
90,
75,
52,
16
];
$VAR2 = [
65,
50,
37,
18
];
$VAR3 = [
90,
81,
74,
50
];
$VAR4 = [
85,
47,
23,
10
];

Output with your solution using map:
$VAR1 = 16;
$VAR2 = 52;
$VAR3 = 75;
$VAR4 = 90;
$VAR5 = [
16,
52,
75,
90,
$VAR5
];
$VAR6 = 18;
$VAR7 = 37;
$VAR8 = 50;
$VAR9 = 65;
$VAR10 = [
18,
37,
50,
65,
$VAR10
];
$VAR11 = 50;
$VAR12 = 74;
$VAR13 = 81;
$VAR14 = 90;
$VAR15 = [
50,
74,
81,
90,
$VAR15
];
$VAR16 = 10;
$VAR17 = 23;
$VAR18 = 47;
$VAR19 = 85;
$VAR20 = [
10,
23,
47,
85,
$VAR20
];
 
yes, it works; just i dunno yet how to make it work with Data::dumper, but without it no problem. just use foreach to print:

foreach (@list){ print &quot;@$_\n&quot;}
 
Watch operator precedence.
[tt]
map { @$_ = sort { $a <=> $b } @$_, $_ } @list;
[/tt]
Since sort() takes a list, (@$_, $_) is the list it's sorting, so the array ref is sorted as an element of the array. To return just the new sorted array ref, you can use ; to separate the sort/assign statement from the return. You could probably do some convoluded trick with parentheses, too, but it looks cleaner with ;

Also, it's faster to just use the default sort routine whenever possible. Since you're doing a simple ascending sort on numbers, just drop it.
[tt]
map { @$_ = sort @$_; $_ } @list;
[/tt]

________________________________________
Andrew - Perl Monkey
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top