Ok, perhaps you need a tutorial on how to dynamically create a hash of data, and how to sort the data. I have constructed a little sample program below which will give you an idea on how to do this.
use strict; use Data::Dumper; # for investigating data structures my %data; # load data into hash of arrays while (<DATA>) { chomp; my @fields = split/,/; $data{$fields[0]} = \@fields; # first column = hash key } print "\%data - \n", Dumper(\%data); # We will transform the hash into a sorted array # sorted on the 3rd column (score) my @array = sort { $a->[2] <=> $b->[2] } map { $data{$_} } keys %data; print "\@array - \n", Dumper(\@array); # Output sorted data lines print "Sorted data - \n"; foreach (@array) { printf "%s\n", join ',', @{$_}; } __DATA__ Sequence1,0001,80 Sequence2,0002,20 Sequence3,0003,40 Sequence4,0004,100 Sequence5,0005,70
The code above will output the following results:
%data - $VAR1 = { 'Sequence1' => [ 'Sequence1', '0001', '80' ], 'Sequence2' => [ 'Sequence2', '0002', '20' ], 'Sequence3' => [ 'Sequence3', '0003', '40' ], 'Sequence4' => [ 'Sequence4', '0004', '100' ], 'Sequence5' => [ 'Sequence5', '0005', '70' ] }; @array - $VAR1 = [ [ 'Sequence2', '0002', '20' ], [ 'Sequence3', '0003', '40' ], [ 'Sequence5', '0005', '70' ], [ 'Sequence1', '0001', '80' ], [ 'Sequence4', '0004', '100' ] ]; Sorted data - Sequence2,0002,20 Sequence3,0003,40 Sequence5,0005,70 Sequence1,0001,80 Sequence4,0004,100
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:
my %data; $data{key} = value;
or
%data->{key} = value;
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):
@sorted_array = map {$_->[0] } sort { $a->[1] <=> $b->[1] } map { [ $_, (split/,/)[2] ] } # retrieve the third key @original_array;
Where @original_array is just an array of data strings.

In reply to Re: sort and hash by Roger
in thread sort and hash by kaweh

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.