[Purdue-pm] How to sort one dimension of a 2d array

Phillip San Miguel pmiguel at purdue.edu
Fri Jan 29 07:45:37 PST 2010


Phillip San Miguel wrote:
> Joe Kline wrote:
>> -----BEGIN PGP SIGNED MESSAGE-----
>> Hash: SHA1
>>
>> Phillip San Miguel wrote:
>>  
>>> This is probably pretty straight forward, but:
>>>
>>> I have a hash of 2d arrays. I want to sort by numerical value across 
>>> one
>>> of these dimensions. What would be the syntax for that?
>>>
>>> $table{$colors}[0..num_of_traces-1][0..num_data_points-1]
>>>
>>> I want sort the values in the first (traces) dimension.
>>>
>>> When I try to write the sort syntax my head starts spinning.
>>>
>>>     
>>
>> wouldn't it just be:
>>
>> @sorted = sort { $a->[0] <=> $b->[0] } @{ $table{$colors} };
>>
>> ?
>>
>> if you want to sort by data points the index for the sort would be [1],
>> wouldn't it?
>>
>> joe
>>
>>   
> I guess I don't understand what that would be doing.
>
> @{ $table{$colors} }
>
> would return a list of arrays of pointers to other arrays.
>
> $b->[0]
> and
> $a->[0]
>
>
> Seems like they would return pointers to the second dimension of the 
> array. So wouldn't they sort the pointers themselves?
>
> Phillip
That said, the syntax you provide, in practice, does do exactly what I 
needed...


More information about the Purdue-pm mailing list