CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
How to sort multidimensional array?

 



gevni
Novice

Nov 28, 2012, 4:50 PM

Post #1 of 10 (2605 views)
How to sort multidimensional array? Can't Post

Hi
I need to sort multidimensional array on the basis of 7 column value. I write this function:

Code
sub sort_1 { 
#the array given is directly modify
my $ref_partition = shift;
my $height=0;
my @array = ();
for(my $j=0;$j <= $#$ref_partition; $j++){
my $col = $ref_partition->[$j];
for(my $i=0;$i <= $#$col; $i++){
my @test = sort { $a->[7] <=> $b->[7] } @{$col->[$i]};
$array[$i] = [$test[0],$height,@test[2..$#test]];
#$i++;
$height += $col->[$i][3];
}
}
$ref_partition = \@array;
}


It give error "Can't use string ("gfhsaa") as an ARRAY ref while "strict refs" "at line my @test = sort $a->[7] <=> $b->[7] } {$col->[$i]};
I think i am doing wrong with array reference. Plz let me know where I am doing the mistake?


rovf
Veteran

Nov 29, 2012, 3:50 AM

Post #2 of 10 (2599 views)
Re: [gevni] How to sort multidimensional array? [In reply to] Can't Post

Most likely, $col is not an array reference. Use the Data::Dumper module to print its content.

BTW, did you run your code with use strict; and use warnings; enabled?


gevni
Novice

Nov 29, 2012, 4:15 AM

Post #3 of 10 (2596 views)
Re: [gevni] How to sort multidimensional array? [In reply to] Can't Post

Yes i am using warning and strict.

Code
sub sort_1 {  
#the array given is directly modify
my $ref_partition = shift;
my $height=0;
my @array = ();
for(my $j=0;$j <= $#$ref_partition; $j++){
my $col_1 = $ref_partition->[$j];
my $col = sort { $a->[7] <=> $b->[7] } @{$col_1};
# After that i need to make $col to have a reference, that i will pass in "i" loop. How i can do it?
for(my $i=0;$i <= $#$col; $i++){
my @test = @{$col->[$i]};
$array[$i] = [$test[0],$height,@test[2..$#test]];
#$i++;
$height += $col->[$i][3];
}
}
$ref_partition = \@array;
}


my $col = sort { $a->[7] <=> $b->[7] } @{$col_1};
After that i need to make $col to have a reference, that i will pass in "i" loop. How i can do it?


hobbes
New User

Nov 29, 2012, 7:43 AM

Post #4 of 10 (2581 views)
Re: [gevni] How to sort multidimensional array? [In reply to] Can't Post

If sorting a multi-dimensional array is all you want to do:

#!/usr/bin/perl -w

use warnings ;
use strict ;

sub sort_1 {
#the array given is directly modify
my $ref_partition = shift;
my @test = sort { $a->[7] <=> $b->[7] } @$ref_partition ;
$ref_partition=\@test ;
}

#Test the subroutine
my $x=[[1, 2, 3, 4, 5, 6, 7, 8, 9], [3, 4, 5, 6, 7, 8, 9, 1, 2], [5, 6, 7, 8, 9, 1, 2, 3, 4]] ;

print "Initial array:\n" ;
foreach my $row (@$x) {
print "@$row\n" ;
}
$x=sort_1 ($x) ;
print "Sorted array:\n" ;
foreach my $row (@$x) {
print "@$row\n" ;
}


gevni
Novice

Nov 29, 2012, 9:33 AM

Post #5 of 10 (2564 views)
Re: [hobbes] How to sort multidimensional array? [In reply to] Can't Post

It works fine in this case but the problem is that $ref_partition is an array of array, in that case how i can handle it for example:

Code
my @value= ($name,@arg);  
$global_array[$j] = [] unless( defined $global_array[$j]);
$global_array[$j][$i] = \@value;
........ some code
and then it return
$partition=\@global_array;

now i pass $partition as a parameter to my function. In this case how i can perform the sort?

sub sort_1 {
#the array given is directly modify
my $ref_partition = shift;
.............
............. #
my previous sort_1 function code
}



Laurent_R
Veteran / Moderator

Nov 29, 2012, 10:27 AM

Post #6 of 10 (2554 views)
Re: [gevni] How to sort multidimensional array? [In reply to] Can't Post

If you want to sort an array of arrays, you have to explain more clearly what you want to sort exactly.

Assume your array contains:

[1, 2, 3, 4, 5, 6, 7, 8, 9], [3, 4, 5, 6, 7, 8, 9, 1, 2], [5, 6, 7, 8, 9, 1, 2, 3, 4]

Do you want to sort [1, 2, 3, 4, 5, 6, 7, 8, 9], and then [3, 4, 5, 6, 7, 8, 9, 1, 2] and finally [5, 6, 7, 8, 9, 1, 2, 3, 4]
? Or do you want something else?

Please provide a data Dumper output of your data (or part thereof) and tell us exactly what you want to do with it.


gevni
Novice

Nov 29, 2012, 11:46 AM

Post #7 of 10 (2547 views)
Re: [Laurent_R] How to sort multidimensional array? [In reply to] Can't Post


Code
sub sort_1 { 
my $ref_partition = shift;
my $height=0;
my @array = ();

for(my $j=0;$j <= $#$ref_partition; $j++){
my $col = sort { my $a->[7] <=> my $b->[7]} $ref_partition->[$j];
for(my $i=0;$i <= $#$col; $i++){
# array = host i j x y w h w*h mem time group_ID
my @test = @{$col->[$i]};
$array[$i] = [$test[0],$height,@test[2..$#test]];
$height += $col->[$i][3];
}
}
$ref_partition = \@array;
}


This function give error at bold line "Useless use of sort in scalar context at ./sort_test.pl line 102." I don't know what i did wrong.


Laurent_R
Veteran / Moderator

Nov 29, 2012, 3:13 PM

Post #8 of 10 (2535 views)
Re: [gevni] How to sort multidimensional array? [In reply to] Can't Post

1. The sort function returns you an array, you want to put this array in a scalar ($col), at best, you probbly won't get what you expect.

2. I do not know what "$ref_partition->[$j]" contains, it might just be a reference to an array, meaning that you will sort a single value, which is not very useful (especially when this value is a memory address).

Again, give us a Dumper print-out of ref_partition, so that we know what it contains, we do not know at this point (an array of arrays? a reference to an array of arrays? something else?).


gevni
Novice

Nov 29, 2012, 4:54 PM

Post #9 of 10 (2532 views)
Re: [Laurent_R] How to sort multidimensional array? [In reply to] Can't Post

Thank you all for your help. @Laurent_R yes you are right. I change it into array and get sorted result. Thanks. I am posting me changed corrected code here so if any one has the same problem, he will get idea.

Code
sub sort_1 { 
my $ref_partition = shift;
my @array = ();
my $i;
my $j;
my @test;
for($j=0;$j <= $#$ref_partition; $j++){
my $col_1= $ref_partition->[$j];
my @col = sort { $a->[7] <=> $b->[7]} @{$col_1};
my $height=0;
for($i=0;$i <= $#col; $i++){
my @test = @{$col[$i]};
$array[$i] = [$test[0],$height,@test[2..$#test]];
$height += $col[$i][3];
}
$ref_partition->[$j] = \@array;
print Dumper $ref_partition->[$j];
}
}


Code
My Damper print value is  
$VAR1 = [
[
'edel-55.0',
0,
'534',
'807',
'430938',
'40401MB',
'7506207.000000',
0
],
[
'edel-62.5',
807,
'534',
'673',
'359382',
'33692MB',
'7501276.000000',
2
],
[
'edel-62.0',
1480,
'534',
'568',
'303312',
'28436MB',
'7487031.000000',
2
]
];
$VAR1 = [
[
'edel-55.1',
0,
'579',
'519',
'300501',
'28172MB',
'7505535.000000',
0
],
[
'edel-62.3',
519,
'579',
'523',
'302817',
'28389MB',
'7500000.000000',
2
],
[
'edel-62.1',
1042,
'579',
'523',
'302817',
'28389MB',
'7500312.000000',
2
]
];
$VAR1 = [
[
'edel-59.1',
0,
'477',
'541',
'258057',
'24193MB',
'7510249.000000',
1
],
[
'edel-59.0',
541,
'477',
'478',
'228006',
'21376MB',
'7497292.000000',
1
],
[
'edel-62.4',
1019,
'477',
'552',
'263304',
'24685MB',
'7485529.000000',
2
]
];

As here i use $ref_partition->[$j] = \@array; So it will overwrite last $j value into array. How i can store all values of &j ?
some thing like:
get sorted value for each &j
store them into array
and pass the referece of that array which has all sorted value of $j into $ref_partition= \@array;
Thank again!!


gevni
Novice

Nov 30, 2012, 3:25 AM

Post #10 of 10 (2525 views)
Re: [gevni] How to sort multidimensional array? [In reply to] Can't Post

Thank you i did it. No need to reply Smile

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives