Algorithm 解析csv文件并跳过前3000行

Algorithm 解析csv文件并跳过前3000行,algorithm,perl,parsing,csv,Algorithm,Perl,Parsing,Csv,我执行此功能是为了修改我的csv文件: sub convert { # open the output/input file my $file = $firstname."_lastname_".$age.".csv"; $file =~ /(.+\/)(.+\.csv)/; my $file_simple = $2; open my $in, '<', $file or die "can not read the file: $file $!"; open my $o

我执行此功能是为了修改我的csv文件:

    sub convert
{
    # open the output/input file 
my $file = $firstname."_lastname_".$age.".csv";
 $file =~ /(.+\/)(.+\.csv)/;
my $file_simple = $2;
open my $in, '<', $file or die "can not read the file: $file $!";
open my $out, '>', $outPut."_lastname.csv" or die "can not open the o file:  $!";

$_ = <$in>;

# first line
print $out "X,Y,Z,W\n";
while( <$in> )
{
    if(/(-?\d+),(-?\d+),(-?\d+),(-?\d+),(-?\d+)/)
    {
        my $tmp = ($4.$5);
        print $out $2.$sep.$3.$sep.$4.$sep.($5/10)."\n";
    }
    else
    {print $out "Error: ".$_;}
}
close $out;
}
子转换
{
#打开输出/输入文件
my$file=$firstname.“\u lastname”.$age..csv”;
$file=~/(.+\/)(.+\.csv)/;
my$file_simple=$2;
打开我的$in、、$outPut.“\u lastname.csv”或die“无法打开o文件:$!”;
$_ = ;
#一线
打印$out“X,Y,Z,W\n”;
而()
{
如果(/(-d+)、(-d+)、(-d+)、(-d+)、(-d+)、(-d+)/)
{
我的$tmp=(4.5美元);
打印$2。$9月$3。$9月$4。$9月($5/10)。“\n”;
}
其他的
{打印$out“错误:“.$\u;}
}
收尾美元;
}
我想跳过前3000行,但我不知道该怎么做,这是我第一次使用perl


谢谢。

既然您希望跳过前3000行,只需在
$的同时使用
下一步即可。

use strict; use warnings;

my $skip_lines = 3001;

open(my $fh, '<', 'data.dat') or die $!;
while (<$fh>) {
    next if $. < $skip_lines;
    //process the file
}
close($fh);
使用严格;使用警告;
我的$skip_行=3001;
打开(my$fh,'输入线路号(EXPR)以访问给定线路的线路计数器
filehandle,而不必担心最后使用哪个句柄
已访问。助记符:许多程序使用“.”表示当前行
号码

参考:


添加一个计数器,如果计数器<3000@Jens;我得到了这个错误光字“co”是不允许的,而“strict subs”在哪一行得到了错误?缺少了变量co的my声明。@Jens;没关系,是我的错,我不明白你的答案,谢谢你:)谢谢,第一次使用下一个if:)