Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Perl中基于时间戳从CSV文件中选择记录?_Perl - Fatal编程技术网

如何在Perl中基于时间戳从CSV文件中选择记录?

如何在Perl中基于时间戳从CSV文件中选择记录?,perl,Perl,我有一个CSV文件,第一列有时间戳,如下示例所示: Time,head1,head2,...head3 00:00:00,24,22,...,n 00:00:01,34,55,...,n and so on... 我想用标题过滤掉特定时间范围的数据,比如从11:00:00到16:00:00,然后放入数组中。我已经编写了以下代码来获取数组中的头 #!/usr/bin/perl -w use strict; my $start = $ARGV[0]; my $end = $ARGV[1]; m

我有一个CSV文件,第一列有时间戳,如下示例所示:

Time,head1,head2,...head3
00:00:00,24,22,...,n
00:00:01,34,55,...,n
and so on...
我想用标题过滤掉特定时间范围的数据,比如从11:00:00到16:00:00,然后放入数组中。我已经编写了以下代码来获取数组中的头

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

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

my $line;
$line =<STDIN>;
my $header = [ split /[,\n]/, $line ];
我需要有关如何从具有选定时间范围的文件中筛选数据并创建该数据的数组的帮助。

只是一个开始

my $start="01:00:01";
my $end = "11:00:01";
while(<>){
    chomp;
    if ( /$start/../$end/ ){
        @s = split /,/ ;
        # do what you want with @s here.
    }
}
只是一个开始

my $start="01:00:01";
my $end = "11:00:01";
while(<>){
    chomp;
    if ( /$start/../$end/ ){
        @s = split /,/ ;
        # do what you want with @s here.
    }
}

我有点作弊。一个合适的程序可能会使用DateTime,然后与DateTime的compare函数进行比较。如果你只希望输入这种格式,我的作弊应该有效

#!/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

我有点作弊。一个合适的程序可能会使用DateTime,然后与DateTime的compare函数进行比较。如果你只希望输入这种格式,我的作弊应该有效

#!/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

请注意,只有当.cvs中的行按时间排序时,上面的代码才会起作用,这似乎是真的,但谁知道呢。是的,它是真的,但它不使用时间戳。当我用“^hour”只给出小时时,它就起作用了。仅对数字表示,但不使用时间戳。如果在这些时间没有日志条目,则不起作用,但考虑得很好。请注意,上面的代码仅在.cvs中的行按时间排序时起作用,这似乎是正确的,但谁知道呢。是的,它是正确的,但不使用时间戳。当我用“^hour”只给出小时时,它就起作用了。只对数字表示,但不带时间戳。如果当时没有日志条目,则不起作用,不过想法不错。