in reply to Sorting issue
You're splitting the line on commas (after changing tabs to commas, which is puzzling), then saving each line in a hash with the key being the first element from your split. So the hash key that you're sorting by is that first column. If you want to sort by something else, you have to tell the sort function that.
To make another column easily available to sort, and to avoid duplicating work you've already done, save @columns in your hash instead of the original line. Then you'll have a hash of arrays, so you can sort on whichever element of the array you'd like:
$tag{$columns[0]} = \@columns; } foreach my $tags ( sort { $tag{$a}[1] <=> $tag{$b}[1] } keys %tag ){
In this case, I'm using <=> to sort numerically, based on the second element of the array pointed to by each hash key's value. To sort alphabetically, change <=> to cmp. Now you can get your array back into @columns with the dereference @{$tag{$tags}}, so you don't have to re-split your line.
One concern: you said you're trying to come up with a unique key for each line, but you're using the first column alone as the key when you put them in the hash. If the values from the first column aren't already unique, you'll be overwriting values there, so lines will already be missing by the time you sort and start adding your other parts. If you need to add the frequency and a random number to get a unique key (and I have a feeling there's a better way to do that than with random numbers, which could repeat), you should do that before you save the key in your hash.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Sorting issue
by bluray (Sexton) on Nov 05, 2011 at 01:45 UTC | |
by aaron_baugher (Curate) on Nov 05, 2011 at 02:54 UTC | |
by Cristoforo (Curate) on Nov 05, 2011 at 03:07 UTC | |
by aaron_baugher (Curate) on Nov 05, 2011 at 13:47 UTC | |
by bluray (Sexton) on Nov 05, 2011 at 14:53 UTC | |
| |
by bluray (Sexton) on Nov 05, 2011 at 03:59 UTC | |
by aaron_baugher (Curate) on Nov 05, 2011 at 13:45 UTC |