B
Ben
Hi,
I have the following arrays, each with the same number of elements.
Each element of @main is associated with each corresponding element of
@b, @c, and @d. i.e., the $main[1] is associated with $b[1],
$x[1], ..., $z[1].
@main = ( 1, 3, 2, 12, 5, 7, 2, 9); # these are likely to be non-
unique floating point values in my real application
@b = ( 1, 1, 2, 1, 1, 3, 1, 3); # the rest of these are
integer values
@x = ( 0, 9, 9, 9, 8, 8, 8, 1);
@d = ( 1, 1, 1, 1, 1, 1, 1, 1);
....
I would like to sort @main numerically in ascending order, and have
@b, @x, @d, ... rearranged according to the new sorted order while
maintaining the original association. Seems like a perfect job for
a hash, I'm just not that comfortable with how to set it up in this
case.
I think that I should create a hash of array references, but I don't
know how to sort the elements (values) of "@main" and subsequently
update the other arrays with the new order. A more brute force
option that occurrs to me is to simply sort the array @main, find the
new "positions" of the sorted array relative to the original and
reorder @b, @x, @d, ... according to the new positions, without using
a hash, but using some loops. Is there a better approach here?
Note: It may be a "completely obvious" different way, I'm a perl
novice, so it is quite probable to find an easy alternate approach.
Additional information:
To populate the hash named "tmp", I'm using:
%tmp = ( );
foreach $x (@main) {
push( @{$tmp{main}}, $x );
push( @{$tmp{b}}, $b[$i] );
push( @{$tmp{x}}, $x[$i] );
push( @{$tmp{d}}, $d[$i] );
...
$i++;
}
I know how to sort a key-value hash by value:
@sorted = sort { $hash{$a} <=> $hash{$b} } keys %hash;
Just not making the next perlogical step in how to do this in the way
described above.
Aside: For populating the hash, I also tried to use the module
Tie::Hash::MultiValue, but it complains that:
Can't locate object method "TIEHASH" via package
"Tie::Hash::Multivalue" at x.pl line 68. No idea what i'm doing
wrong. I installed Tie::Hash::Multivalue following the instructions
contained in the module's README file, no error messages or warnings
running perl -w. I'm not using strict.
Thanks for your advice.
-Ben
I have the following arrays, each with the same number of elements.
Each element of @main is associated with each corresponding element of
@b, @c, and @d. i.e., the $main[1] is associated with $b[1],
$x[1], ..., $z[1].
@main = ( 1, 3, 2, 12, 5, 7, 2, 9); # these are likely to be non-
unique floating point values in my real application
@b = ( 1, 1, 2, 1, 1, 3, 1, 3); # the rest of these are
integer values
@x = ( 0, 9, 9, 9, 8, 8, 8, 1);
@d = ( 1, 1, 1, 1, 1, 1, 1, 1);
....
I would like to sort @main numerically in ascending order, and have
@b, @x, @d, ... rearranged according to the new sorted order while
maintaining the original association. Seems like a perfect job for
a hash, I'm just not that comfortable with how to set it up in this
case.
I think that I should create a hash of array references, but I don't
know how to sort the elements (values) of "@main" and subsequently
update the other arrays with the new order. A more brute force
option that occurrs to me is to simply sort the array @main, find the
new "positions" of the sorted array relative to the original and
reorder @b, @x, @d, ... according to the new positions, without using
a hash, but using some loops. Is there a better approach here?
Note: It may be a "completely obvious" different way, I'm a perl
novice, so it is quite probable to find an easy alternate approach.
Additional information:
To populate the hash named "tmp", I'm using:
%tmp = ( );
foreach $x (@main) {
push( @{$tmp{main}}, $x );
push( @{$tmp{b}}, $b[$i] );
push( @{$tmp{x}}, $x[$i] );
push( @{$tmp{d}}, $d[$i] );
...
$i++;
}
I know how to sort a key-value hash by value:
@sorted = sort { $hash{$a} <=> $hash{$b} } keys %hash;
Just not making the next perlogical step in how to do this in the way
described above.
Aside: For populating the hash, I also tried to use the module
Tie::Hash::MultiValue, but it complains that:
Can't locate object method "TIEHASH" via package
"Tie::Hash::Multivalue" at x.pl line 68. No idea what i'm doing
wrong. I installed Tie::Hash::Multivalue following the instructions
contained in the module's README file, no error messages or warnings
running perl -w. I'm not using strict.
Thanks for your advice.
-Ben