0
my @someones;
my $xpath = q(//body[@id='ext-gen3']/div[14]/div[2]/div/div/div/div/div/div/div/div/div/div[2]/div/div/table/tbody/tr/td/div/div);   
for my $i (0,2..20)  
{      
my @nodes = split qr'/', $xpath;      
$nodes[16] .= "[$i]" unless 0 == $i;      
#say join '/', @nodes; } 
@somenames = join('/',@nodes); 
print "\n"; 
print @somenames ; 
}

输出:

//body[@id='ext-gen3']/div[14]/div[2]/div/div/div/div/div/div/div/div/div/div[2]
/div/div/table/tbody/tr/td/div/div
//body[@id='ext-gen3']/div[14]/div[2]/div/div/div/div/div/div/div/div/div/div[2]
/div/div[2]/table/tbody/tr/td/div/div
//body[@id='ext-gen3']/div[14]/div[2]/div/div/div/div/div/div/div/div/div/div[2]
/div/div[3]/table/tbody/tr/td/div/div
//body[@id='ext-gen3']/div[14]/div[2]/div/div/div/div/div/div/div/div/div/div[2]
/div/div[4]/table/tbody/tr/td/div/div
//body[@id='ext-gen3']/div[14]/div[2]/div/div/div/div/div/div/div/div/div/div[2]
/div/div[5]/table/tbody/tr/td/div/div
//body[@id='ext-gen3']/div[14]/div[2]/div/div/div/div/div/div/div/div/div/div[2]
/div/div[6]/table/tbody/tr/td/div/div
//body[@id='ext-gen3']/div[14]/div[2]/div/div/div/div/div/div/div/div/div/div[2]
/div/div[7]/table/tbody/tr/td/div/div
//body[@id='ext-gen3']/div[14]/div[2]/div/div/div/div/div/div/div/div/div/div[2]
/div/div[8]/table/tbody/tr/td/div/div
//body[@id='ext-gen3']/div[14]/div[2]/div/div/div/div/div/div/div/div/div/div[2]
/div/div[9]/table/tbody/tr/td/div/div
//body[@id='ext-gen3']/div[14]/div[2]/div/div/div/div/div/div/div/div/div/div[2]
/div/div[10]/table/tbody/tr/td/div/div
//body[@id='ext-gen3']/div[14]/div[2]/div/div/div/div/div/div/div/div/div/div[2]
/div/div[11]/table/tbody/tr/td/div/div
//body[@id='ext-gen3']/div[14]/div[2]/div/div/div/div/div/div/div/div/div/div[2]
/div/div[12]/table/tbody/tr/td/div/div
//body[@id='ext-gen3']/div[14]/div[2]/div/div/div/div/div/div/div/div/div/div[2]
/div/div[13]/table/tbody/tr/td/div/div
//body[@id='ext-gen3']/div[14]/div[2]/div/div/div/div/div/div/div/div/div/div[2]
/div/div[14]/table/tbody/tr/td/div/div
//body[@id='ext-gen3']/div[14]/div[2]/div/div/div/div/div/div/div/div/div/div[2]
/div/div[15]/table/tbody/tr/td/div/div
//body[@id='ext-gen3']/div[14]/div[2]/div/div/div/div/div/div/div/div/div/div[2]
/div/div[16]/table/tbody/tr/td/div/div
//body[@id='ext-gen3']/div[14]/div[2]/div/div/div/div/div/div/div/div/div/div[2]
/div/div[17]/table/tbody/tr/td/div/div
//body[@id='ext-gen3']/div[14]/div[2]/div/div/div/div/div/div/div/div/div/div[2]
/div/div[18]/table/tbody/tr/td/div/div
//body[@id='ext-gen3']/div[14]/div[2]/div/div/div/div/div/div/div/div/div/div[2]
/div/div[19]/table/tbody/tr/td/div/div
//body[@id='ext-gen3']/div[14]/div[2]/div/div/div/div/div/div/div/div/div/div[2]
/div/div[20]/table/tbody/tr/td/div/div

执行上述代码片段后,所有 20 个值都存储在单个元素中(即 $somenames[0])。但我想存储在每个数组元素中,for i=0,$somenames[0]; for i=2,$somenames[2];直到for i=20,$somenames[20]。我该怎么做 ?而且如果我在for循环之外打印@somenames,它只会打印第20个元素。如何将每个值存储在数组的每个元素中?

4

3 回答 3

2

你的问题令人困惑。我不确定你想要完成什么。也许这就是你想要的:

# Assign the right side to the $i'th element of @somenames
$somenames[$i] = join('/',@nodes); 

或者

# Add an element to the end of @somenames
push @somenames, join('/',@nodes);
于 2012-06-27T19:53:57.497 回答
0

请参阅此代码以向您展示如何将数组存储到数组 >>

脚本:

use Data::Dumper;

my @a;

@{$a[0]} = (1, 2, 3);
@{$a[1]} = (5, 6, 7);

print Dumper(@a);

输出:

$VAR1 = [
          1,
          2,
          3
        ];
$VAR2 = [
          5,
          6,
          7
        ];

在这里测试上面的代码。

于 2012-06-27T19:17:20.867 回答
0

push填充一个数组。例如:

perl -le '$str="1 2 3 4 5";push @vals,split / /,$str;print join ":",@vals'
1:2:3:4:5
于 2012-06-27T19:34:06.667 回答