tags:

views:

96

answers:

3

I have CSV file which has timestamp in the first column as shown below sample:

Time,head1,head2,...head3
00:00:00,24,22,...,n
00:00:01,34,55,...,n
and so on...

I want to filter out the data with specific time range like from 11:00:00 to 16:00:00 with the header and put into an array. I have written the below code to get the header in an array.

#!/usr/bin/perl -w
use strict;

my $start = $ARGV[0];
my $end = $ARGV[1];

my $line;
$line =<STDIN>;
my $header = [ split /[,\n]/, $line ];

I need help on how to filter data from file with selected time range and create an array of that.

+1  A: 

just a start

my $start="01:00:01";
my $end = "11:00:01";
while(<>){
    chomp;
    if ( /$start/../$end/ ){
        @s = split /,/ ;
        # do what you want with @s here.
    }
}
ghostdog74
Please note that the code above will work only if lines in .cvs are sorted by time, which seems to be true, but who knows.
Igor Korkhov
yes its true, but its not working with time stamp. Its working when I give only the hour with "^hour". Mean only for numbers but not with time stamp.
Space
Won't work if there are no log entries at those times, nice thinking though.
Dean Povey
+1  A: 
#!/usr/bin/perl -w
use strict;

my $start = '11:00:00';
my $end = '16:00:00';

my @data;
chomp ($_ = <STDIN>);    # remove trailing newline character
push @data, [split /,/]; # add header

while(<>) {
    chomp;
    my @line = split /,/; 
    next if $line[0] lt $start or $line[0] gt $end;
    push @data, [@line]; # $data[i][j] contains j-th element of i-th line.
}
Igor Korkhov
+2  A: 

I kind of cheated. A proper program would probably use DateTime and then compare with DateTime's compare function. If you're only expecting input in this format, my "cheat" should work.

#!/usr/bin/perl
use strict;
use warnings;

use Text::CSV;
use DateTime;

my $start = 110000;
my $end   = 160000;

my $csv = Text::CSV->new () or die "Cannot use CSV: ".Text::CSV->error_diag ();

my @lines;
open my $fh, "<:encoding(utf8)", "file.csv" or die "Error opening file: $!";
while ( my $row = $csv->getline( $fh ) ) {
    my $time = $row->[0];
    $time =~ s/\://g;
    if ($time >= $start and $time <= $end) {
        push @lines, $row;
    }
}
$csv->eof or $csv->error_diag();
close $fh;

#do something here with @lines
Matt Siegman