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

Bradley Andersen bradley.d.andersen at gmail.com
Fri Jan 29 07:47:27 PST 2010


I have this habit of not sending code I have not tested ... perhaps
Joe works in the same manner?

;-P



On Fri, Jan 29, 2010 at 10:45 AM, Phillip San Miguel <pmiguel at purdue.edu> wrote:
> 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...
> _______________________________________________
> Purdue-pm mailing list
> Purdue-pm at pm.org
> http://mail.pm.org/mailman/listinfo/purdue-pm
>


More information about the Purdue-pm mailing list