【发布时间】:2014-12-03 14:25:54
【问题描述】:
我想从 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
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 ',', @$_;
}
【问题讨论】:
-
您向我们展示了一点XY problem。你到底想用你的代码做什么?根据第三个字段对 CSV 中的行进行排序?
-
是的,这正是我想要做的。很抱歉出现 XY 问题,我什至不知道我在做什么!
-
有什么问题?任何输出?