【发布时间】:2013-11-12 20:18:13
【问题描述】:
我们在 Unix 中使用的一些环境变量如下(只是一个例子):
VAR1=variable1
VAR2=variable2
VAR3=variable3
# and so on
现在,我有一个 perl 脚本(我们称之为 test.pl),它读取一个制表符分隔的文本文件(我们称之为 test.txt)并将其内容按列推送到单独的数组中。test.txt 的第一列例如包含以下信息(第一列中的字符串由/ 分隔,但我不知道/ 字符串将如何包含以及环境变量将出现在什么位置):
$VAR1/$VAR2/$VAR3
$VAR3/some_string/SOME_OTHER_STRING/and_so_on/$VAR2
$VAR2/$VAR1/some_string/some_string_2/some_string_3/some_string_n/$VAR2
脚本摘录如下:
use strict;
my $input0 = shift or die "must provide test.txt as the argument 0\n";
open(IN0,"<",$input0) || die "Cannot open $input0 for reading: $!";
my @first_column;
while (<IN0>)
{
chomp;
my @cols = split(/\t/);
my $first_col = `eval $cols[0]`; #### but this does not work
# here goes the push stmt to populate the array
### more code here
}
close(IN0);
问题:在这种情况下如何访问环境变量,以便数组填充如下:
$first_column[0] = variable1/vraible2/variable3
$first_column[1] = variable3/some_string/SOME_OTHER_STRING/and_so_on/variable2
$first_column[2] = variable2/variable1/some_string/some_string_2/some_string_3/some_string_n/variable2
【问题讨论】:
-
你的问题没有意义,请澄清.....
-
我已经展示了示例输入并且我已经展示了想要的结果。我现在还应该添加什么?
-
您是否也允许像 ${VAR1:-foo} 这样的单词的正常 shell 扩展或像 ${VAR1//foo/bar} 这样的复杂扩展(即 bashisms)?
标签: perl