2012-06-27 181 views
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個元素,將每個值存儲在數組的每個元素中?

回答

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); 
+0

@唐迪邁克爾間質:其實我錯過了一個$符號eithin []。謝謝你 :) –

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 
     ]; 

測試上面的代碼here

0

push填充數組。例如:

perl -le '$str="1 2 3 4 5";push @vals,split//,$str;print join ":",@vals' 
1:2:3:4:5 
相關問題