开发者

How can I select records from a CSV file based on timestamps in Perl?

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 sel开发者_Python百科ected time range and create an array of that.


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


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.
    }
}


#!/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.
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜