Vijay Kumar Adhikari <[EMAIL PROTECTED]> asked:
> I have some tab delimited text data like
>
> ID In Out Day
> 1 5 2 1
> 2 4 9 2
> 3 3 3 2
> 4 6 7 3
> 5 5 0 5
> 6 7 9 3
> 7 8 9 4
> 8 6 6 4
>
> I want to perform sorting by In, Out and so on. I also want
> to find sum of in out grouped by Day. Where should I start?
> Is there any module that may help me? I am a beginner.
This has "SQL" written all over it ;-)
If you wanted to do it in Perl, the obvious way would
be to read in the file line by line and turn it into
an hash of arrays, i.e.
#!/usr/bin/perl
use strict;
use warnings;
# read first line
my $line = <DATA>;
my %rec;
# sorting functions
sub by_id {
return $rec{$a}->[0] <=> $rec{$b}->[0];
}
sub by_in {
return $rec{$a}->[1] <=> $rec{$b}->[1];
}
sub by_out {
return $rec{$a}->[2] <=> $rec{$b}->[2];
}
sub by_day {
return $rec{$a}->[3] <=> $rec{$b}->[3];
}
# list output
sub print_list {
print "ID\tIn\tOut\tDay\n";
foreach my $id (@_){
printf "%d\t%d\t%d\t%d\n", @{$rec{$id}};
}
print "\n";
}
# read in data
while( $line = <DATA> ){
my( @data ) = split /\s+/, $line;
@{$rec{$data[0]}} = @data;
}
# print out sorted lists
print_list sort by_in keys %rec;
print_list sort by_out keys %rec;
print_list sort by_day keys %rec;
__DATA__
ID In Out Day
1 5 2 1
2 4 9 2
3 3 3 2
4 6 7 3
5 5 0 5
6 7 9 3
7 8 9 4
8 6 6 4
I recommend you read the perllol manpage for the
list-of-lists syntax details, and the perlfaq on
sorting (perldoc -q sort).
HTH,
Thomas
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>