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新手-使用perl从csv动态输入数组(列)的值_Perl_Perl Module_Mod Perl - Fatal编程技术网

perl新手-使用perl从csv动态输入数组(列)的值

perl新手-使用perl从csv动态输入数组(列)的值,perl,perl-module,mod-perl,Perl,Perl Module,Mod Perl,我是perl新手,需要编写具有以下要求的perl脚本: 需要读取csv文件吗 将列存储在数组中 假设csv中有7个字段(列):字段1、字段2、字段3、字段4、字段5、字段6、字段7 我需要能够动态地将任何字段作为输入参数。假设输入参数为字段3、字段7,csv中的数据为: No of orders'|Date'|Year'|Exp_date'|Month'|time'|Committed 12'|12122002'|2013'|02022012'|12'|1230'|Yes 然后我希望输出为:

我是perl新手,需要编写具有以下要求的perl脚本:

  • 需要读取csv文件吗
  • 将列存储在数组中
  • 假设csv中有7个字段(列):字段1、字段2、字段3、字段4、字段5、字段6、字段7
  • 我需要能够动态地将任何字段作为输入参数。假设输入参数为字段3、字段7,csv中的数据为:

    No of orders'|Date'|Year'|Exp_date'|Month'|time'|Committed
    12'|12122002'|2013'|02022012'|12'|1230'|Yes
    
    然后我希望输出为:

    Year~Committed                          
    2013~Yes 
    
    其余列的格式也应相同:

    No of orders~Date~Exp_date~Month~time
    12~12122002~02022012~12~1230
    
    目前,我从net获得了一个perl脚本,它只以硬编码格式向我提供左侧结果。但我想在运行时提供输入,并希望生成结果。非常感谢您的帮助

    $filename = 'xyz.csv';
    
    # use the perl open function to open the file
    open(FILE, $filename) or die "Could not read from $filename, program halting.";
    
    # loop through each line in the file
    # with the typical "perl file while" loop
    while(<FILE>)
    {
    
      chomp;
    
      # read the fields in the current line into an array
      @fields = split('\`\|', $_);
    
     # print the concat
     print "$fields[0]~$fields[1]\n";
    }
    
    
    close FILE;
    
    $filename='xyz.csv';
    #使用perl open函数打开文件
    打开(文件,$filename)或死亡“无法读取$filename,程序停止。”;
    #循环浏览文件中的每一行
    #使用典型的“perl文件while”循环
    while()
    {
    咀嚼;
    #将当前行中的字段读入数组
    @字段=拆分(“\`\\”,$);
    #打印海螺
    打印“$fields[0]~$fields[1]\n”;
    }
    关闭文件;
    
    我不会争论是否使用Text::CSV。这与问题无关,
    split
    完全可以接受

    假设海报想要有多行数据的输入文件,我将如何解决这个问题

    #!/usr/bin/perl -w
    
    # take any file name from the command line, instead of hard coding it
    my ($filename) = @ARGV;
    my @title;
    my @table;
    
    open FILE, "<", $filename or die "Could not read from $filename, program halting.";
    while (<FILE>) {
      chomp;
      if (!@title) {
        @title = split '\'\|';
        next;
      }
      my @row = split '\'\|';
      # build an array of array references, a two dimensional table
      push @table, \@row;
    }
    close FILE;
    
    print "Access which row? ";
    my $row = <STDIN> - 1;
    die "Row value is out of range for $filename\n" if (($row < 0) || ($row >= scalar(@table)));
    
    print "Access which column? ";
    my $col = <STDIN> - 1;
    die "Column value is out of range for $filename\n" if (($col < 0) || ($col >= scalar(@title)));
    
    print "$title[$col]\~$table[$row][$col]\n";
    
    #/usr/bin/perl-w
    #从命令行获取任何文件名,而不是硬编码
    我的($filename)=@ARGV;
    我的@title;
    我的@table;
    
    打开文件,“不要尝试用正则表达式解析CSV。改为使用
    Text::CSV
    。但即使使用Text::CSV,我也可以将列作为输入参数吗?
    Text::CSV
    逐行解析CSV文件,为您提供一个字段数组。在此脚本中,它将取代您的
    split()
    上面的命令。之后如何操作字段取决于您自己。