kaweh has asked for the wisdom of the Perl Monks concerning the following question:
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: sort and hash
by jeffa (Bishop) on Sep 22, 2003 at 22:58 UTC | |
Let's say you have a hash like so: You can add more key/value pairs like so: Let's say that your id is stored in the variable $id and the score is stored in the variable $score. You can add this information to the hash like so: If you want to sort the data by say, the id, you can get a list of the keys with the keys built-in function and use the sort built-in function, specifying that you want to sort numerically with the "spaceship" operator <=> like so: Perl's motto is 'There Is More Than One Way To Do It'. Give us some more context, like what these id's and scores really look like and we can help even more. jeffa L-LL-L--L-LL-L--L-LL-L-- -R--R-RR-R--R-RR-R--R-RR B--B--B--B--B--B--B--B-- H---H---H---H---H---H--- (the triplet paradiddle with high-hat) | [reply] [d/l] [select] |
by kaweh (Initiate) on Sep 22, 2003 at 23:14 UTC | |
| [reply] |
by jeffa (Bishop) on Sep 23, 2003 at 00:51 UTC | |
Limbic~Region metioned to me that you might be wanting to preserve the sort, that is, the keys stay sorted even after you add new ones. You can use the CPAN module Tie::Hash::Sorted for this, but first see A Guide to Installing Modules if you are not familiar with the CPAN. Tie's are, in my opinion, a bit hard to grasp if you are new to Perl (see perltie), but here goes anyway. :) Data::Dumper is another CPAN module, but it comes with your Perl installation so you don't have to install it like you do Tie::Hash::Sorted. Once you have \ installed it, run the above example and notice the output. Note that even though your hash stays 'magically' sorted, there are a lot of CPU cycles burned to achieve it. In other words, if you really don't need to keep the hash sorted, then don't don't keep it sorted. Also, don't rule out using an array. Hashes are really good for quick lookups. If you are more concerned with sorting then looking up items, use an array. jeffa L-LL-L--L-LL-L--L-LL-L-- -R--R-RR-R--R-RR-R--R-RR B--B--B--B--B--B--B--B-- H---H---H---H---H---H--- (the triplet paradiddle with high-hat) | [reply] [d/l] |
|
Re: sort and hash
by Roger (Parson) on Sep 23, 2003 at 01:06 UTC | |
The code above will output the following results: Where %data contains a hash of arrays of data. @array is a sorted array of array of data. And use join to put the array of data back to its original form. To insert a key/value pair into a hash, use the syntax below: or The line of code my @array = sort { $a->[2] <=> $b->[2] } map { $data{$_} } keys %data; translates to English: Retrieve the data arrays of for each key in the %data hash, sort the data arrays based on the 3rd data column, and put them in the new array @array. And thus this will give you a sorted array of array of data. If you only want to sort the data based on a certain column, why not try out the Schwartzian Transform invented by Randal Schwartz (merlin): Where @original_array is just an array of data strings. | [reply] [d/l] [select] |
|
Re: sort and hash
by stajich (Chaplain) on Sep 23, 2003 at 01:11 UTC | |
| [reply] [d/l] |
|
Re: sort and hash
by dmitri (Priest) on Sep 22, 2003 at 22:56 UTC | |
Maybe 'I want to use a hash' is not a good approach here. It seems you'd be better off using a sorted array for your nodes and use binary search to insert new items. | [reply] |