我将这些数据加载到一个数组中,打印出来确定。然而,我想要做的是逐行读取文件,然后将整行加载到另一个数组,然后打印出第二个数组的第二个字段。
#!/usr/bin/perl
use strict;
use warnings;
my @fields;
while ( my $line = <DATA> ) {
@fields = split /,/, $line;
foreach my $field ( @fields ) {
push ( my @one_line_array, $field );
print my "$one_line_array[1]\n" ;
}
}
__DATA__
BACKUP, CAPSER_JOB_01_PP, usa-penguin.com, /bin/bash -lc '/usr/bin/run.sh'
PROD, CAPSER_PROD_JOB_01_PS, usa-penguin.com, /bin/bash -lc '/usr/bin/run.sh'
BACKUP, CAPSER_JOB_02_PP, usa-penguin.com, /bin/bash -lc '$HOME/run/script02'
PROD, CAPSER_PROD_JOB_02_PS, usa-penguin.com, /bin/bash -lc '$HOME/run/script02'
BACKUP, CAPSER_JOB_03_PP, usa-penguin.com, /bin/bash -lc '$HOME/run/script03'
PROD, CAPSER_PROD_JOB_03_PS, usa-penguin.com, /bin/bash -lc '$HOME/run/script03'
BACKUP, CAPSER_JOB_04_PP, usa-penguin.com, /bin/bash -lc '$HOME/run/script04'
PROD, CAPSER_PROD_JOB_04_PS, usa-penguin.com, /bin/bash -lc '$HOME/run/script04'
PROD, CAPSER_PROD_JOB_05_PS, usa-penguin.com, /bin/bash -lc '$HOME/run/script05'
PROD, CAPSER_PROD_JOB_06_PS, usa-penguin.com, /bin/bash -lc '$HOME/run/script06'
BACKUP, CAPSER_JOB_07_PP, usa-penguin.com, /bin/bash -lc '$HOME/run/script07'
PROD, CAPSER_PROD_JOB_07_PS, usa-penguin.com, /bin/bash -lc '$HOME/run/script07'
BACKUP, CAPSER_JOB_08_PP, usa-penguin.com, /bin/bash -lc '$HOME/run/script08'
PROD, CAPSER_PROD_JOB_08_PS, usa-penguin.com, /bin/bash -lc '$HOME/run/script08'
BACKUP, CAPSER_JOB_09_PP, usa-penguin.com, /bin/bash -lc '$HOME/run/script09'
PROD, CAPSER_PROD_JOB_09_PS, usa-penguin.com, /bin/bash -lc '$HOME/run/script09'
BACKUP, CAPSER_JOB_10_PP, usa-penguin.com, /bin/bash -lc '$HOME/run/script10'
PROD, CAPSER_PROD_JOB_10_PS, usa-penguin.com, /bin/bash -lc '$HOME/run/script10'
BACKUP, CAPSER_JOB_11_PP, usa-penguin.com, /bin/bash -lc '$HOME/run/script11'
PROD, CAPSER_PROD_JOB_11_PS, usa-penguin.com, /bin/bash -lc '$HOME/run/script11'
PROD, CAPSER_PROD_JOB_12_PS, europe-linux.com, /bin/bash -lc '$HOME/run/script12'
PROD, CAPSER_PROD_JOB_13_PS, europe-linux.com, /bin/bash -lc '$HOME/run/script13'
BACKUP, CAPSER_JOB_14_PP, usa-penguin.com, /bin/bash -lc '$HOME/run/script14'
BACKUP, CAPSER_JOB_15_PP, usa-penguin.com, /bin/bash -lc '$HOME/run/script15'
BACKUP, CAPSER_JOB_16_PP, usa-penguin.com, /bin/csh -c '$HOME/TRADEDATE.pl'
PROD, CAPSER_PROD_JOB_17_PS, usa-penguin.com, /bin/csh -c '$HOME/TRADEDATE.pl'
BACKUP, CAPSER_JOB_01_17, usa-penguin.com, /bin/bash -lc '$HOME/run/script17'
PROD, CAPSER_PROD_JOB_18_PS, usa-penguin.com, /bin/bash -lc '$HOME/run/script18'
BACKUP, CAPSER_JOB_19_PP, usa-penguin.com, /bin/bash -lc '$HOME/run/script19'
当我打印出第一个数组时,我可以打印出来 - 这样就可以了:
BACKUP
CAPSER_JOB_01_PP
usa-penguin.com
/bin/bash -lc '/usr/bin/run.sh'
PROD
CAPSER_PROD_JOB_01_PS
usa-penguin.com
/bin/bash -lc '/usr/bin/run.sh'
BACKUP
CAPSER_JOB_02_PP
usa-penguin.com
/bin/bash -lc '$HOME/run/script02'
PROD
CAPSER_PROD_JOB_02_PS
usa-penguin.com
/bin/bash -lc '$HOME/run/script02'
然而,当我创建第二个数组时,我想要打印的只是该数组的第二个字段:
CAPSER_JOB_01_PP
CAPSER_PROD_JOB_01_PS
CAPSER_JOB_02_PP
CAPSER_PROD_JOB_02_PS
当我尝试获取第二个数组的第二个字段时,我收到了这些错误。当我用&#34;我的&#34;初始化它时我收到编译错误。我知道如何切出每一秒的场。
casper@casper.com 1066$ ./casper_test2.pl
Use of uninitialized value $one_line_array[1] in concatenation (.) or string at ./casper_test2.pl line 13, <DATA> line 1.
Use of uninitialized value $one_line_array[1] in concatenation (.) or string at ./casper_test2.pl line 13, <DATA> line 2.
Use of uninitialized value $one_line_array[1] in concatenation (.) or string at ./casper_test2.pl line 13, <DATA> line 2.
Use of uninitialized value $one_line_array[1] in concatenation (.) or string at ./casper_test2.pl line 13, <DATA> line 2.
Use of uninitialized value $one_line_array[1] in concatenation (.) or string at ./casper_test2.pl line 13, <DATA> line 2.
casper@casper.com 1068$ ./casper_test2.pl
syntax error at ./casper_test2.pl line 13, near "my "$one_line_array[1]\n""
Execution of ./casper_test2.pl aborted due to compilation errors.
casper@casper.com1069$
@Diab Jerius - 这实际上给了我现在所需要的东西:
while (my $line = <DATA> ) {
@fields = split /,/, $line ;
print "$fields[1]\n" ;
}
答案 0 :(得分:0)
我看到的问题在下面一行:
print my "$one_line_array[1]\n" ; #Why you using my here...!
其次,您可以直接在循环内部尝试打印$ field [1],看看您是否获得了正确的值。
#!/usr/bin/perl
use strict;
use warnings;
my @fields;
while ( my $line = <DATA> ) {
@fields = split /,/, $line;
print $fields[1];
}
...更新
答案 1 :(得分:0)
如果您想要的只是第二个字段,那么您可以使用map
:
use strict;
use warnings;
use Data::Dumper;
my @second_col = map { (split /,/) [1] } <DATA>;
print Dumper \@second_col;
如果那是你的用例,你也可以同时对它进行子串/变换:
my @trimmed_second_col = map { (split /,/)[1] =~ s/\s+//gr } <DATA>;
print Dumper \@trimmed_second_col;
这将按<DATA>
的一行进行迭代,在,
上拆分每一个,然后应用'删除空白'正则表达式,这要归功于r
标志返回结果,以及存储到@trimmed_second_col
。
但是我认为你遇到问题的原因是因为你误解了一些事情 - 你正在推动循环的每次迭代,这意味着你做了太多次。你误导了my
。
my
所做的是声明一个对当前范围是私有的变量。因此,如果你在一个foreach循环中使用它,它每次都会创建一个 new 数组,当循环迭代时它会再次消失。特别是print
毫无意义。
那怎么样:
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dumper;
my @second_col;
while ( <DATA> ) {
my @row = split /,/;
# if you need to preserve it in an array.
push @second_col, $row[1];
#or if you just need to print it:
print $row[1],"\n";
}
print Dumper \@second_col;