用Perl从文件中输入列表

用Perl从文件中输入列表,perl,list,file,text,input,Perl,List,File,Text,Input,我想用Perl从列表中输入数据。到目前为止,我一直在将数据列表粘贴到实际的程序中,以便它进行处理。我最初的排序程序如下所示: # /perl/bin use strict; #force perl to code properly use warnings; #find typing mistakes in program like missing semicolons, etc. use Text::ParseWords; #parse text into an array of tok

我想用Perl从列表中输入数据。到目前为止,我一直在将数据列表粘贴到实际的程序中,以便它进行处理。我最初的排序程序如下所示:

# /perl/bin


use strict; #force perl to code properly
use warnings; #find typing mistakes in program like missing semicolons, etc.

use Text::ParseWords; #parse text into an array of tokens or array of arrays

my @rows;

while (<DATA>) {
    push @rows, [ parse_line(',', 0, $_) ];
}

@rows = sort { $a->[2] <=> $b->[2] } @rows;

open OUTPUT, ">OUTPUT.TXT";

foreach (@rows) {
    print OUTPUT join ',', @$_;
}

__DATA__
SMITH,M,1
JONES,F,1
...
#/perl/bin
严格使用#强制perl正确编码
使用警告#查找程序中的键入错误,如缺少分号等。
使用Text::ParseWords#将文本解析为令牌数组或数组数组
我的@行;
而(){
按@行[parse_line(',',0,$)];
}
@rows=sort{$a->[2]$b->[2]}@rows;
打开输出,“>OUTPUT.TXT”;
foreach(@行){
打印输出联接“,”,@$;
}
__资料__
史密斯,M,1岁
琼斯,F,1
...
但是我想让它从一个有这个列表的文件中输入。我甚至不确定自己是否走上了正确的道路,但这就是我目前的情况:

# /perl/bin


use strict;                     #force perl to code properly
use warnings;                   #find typing mistakes in program like missing semicolons, etc.
use autodie;                    #replace functions with ones that succeed or die with lexical scope

use Text::ParseWords;               #parse text into an array of tokens or array of arrays

open(MYINPUTFILE, "<inputfile.txt");        # open for input

my @rows = <MYINPUTFILE>;               # read file into list

while (<MYINPUTFILE>) {
    push @rows, [ parse_line(',', 0, $_) ];
}

@rows = sort { $a->[2] <=> $b->[2] } @rows;

open OUTPUT, ">OUTPUT.TXT";

foreach (@rows) {
    print OUTPUT join ',', @$_;
}
#/perl/bin
严格使用#强制perl正确编码
使用警告#查找程序中的键入错误,如缺少分号等。
使用自动模具#用词法作用域成功或失败的函数替换函数
使用Text::ParseWords#将文本解析为令牌数组或数组数组
打开(MYINPUTFILE,“OUTPUT.TXT”;
foreach(@行){
打印输出联接“,”,@$;
}

以下是问题的症结所在:

open(MYINPUTFILE, "<inputfile.txt");        # open for input

my @rows = <MYINPUTFILE>;               # read file into list

open(MYINPUTFILE),“您给我们带来了一点麻烦。您到底想用代码做什么?根据第三个字段对CSV中的行进行排序?是的,这正是我想做的。很抱歉XY问题,我甚至不知道我在做什么!有什么问题吗?有输出吗?请看,它应该是
open(…)或者die
或者
使用autodie;
@Axeman OP在他们的“这就是我目前所拥有的”脚本中使用
autodie
。@ialarmedalien.错过了。谢谢。
while (<MYINPUTFILE>) {                 # try to read file again,
                                        # but you've already read it all
    push @rows, [ parse_line(',', 0, $_) ];
}
open(MYINPUTFILE, "<inputfile.txt");        # open for input

my @rows;

while (<MYINPUTFILE>) {
    push @rows, [ parse_line(',', 0, $_) ];
}