2014-02-27 57 views
4

我正在讀取csv文件,並且需要將行(第4行)中的值作爲數據庫中的關鍵元素。但該行包含逗號分隔的多個值。Perl:將元素推入到數組中用新變量值替換現有值

  1. 我使用Text :: CSV解析文件並將值分成第4行。

  2. 然後將這些值插入一個數組並插入到一個新文件中,並保持其他值相同。

  3. 但在循環的下一次運行中,值被替換爲新值。

    use Data::Dumper; 
    use strict; 
    my @oneRow = ('Vehicle Factory', 
          'D3', 
          '2518, 1613, 1512, 1109, 912 bus, 712 bus, 613 Export', 
          'ajj137035, mgp657301', 
          'ddb255570', 
          'mdb650204' 
         ); 
    my $row = \@oneRow; 
    my @newRows; 
    my $userString = $row->[3]; 
         my @userNewRow = split(/,/,$userString); 
         foreach(@userNewRow) { 
          $row->[3] =~ s/.*/$_/; 
          print Dumper $row; 
          push @newRows, $row; 
          print Dumper @newRows; 
         } 
    

    自卸車結果是:

  4. 因此我

的代碼(例如低於2)結束了的最後一個值的數組中的許多實例

#comment: this is Dumper $row result of first run in loop 
$VAR1 = [ 
      'Vehicle Factory', 
      'D3', 
      '2518, 1613, 1512, 1109, 912 bus, 712 bus, 613 Export', 
      'ajj137035', 
      'ddb255570', 
      'mdb650204' 
     ]; 
#comment: this is the Dumper @newRows result of first run in loop 
$VAR1 = [ 
      'Vehicle Factory', 
      'D3', 
      '2518, 1613, 1512, 1109, 912 bus, 712 bus, 613 Export', 
      'ajj137035', 
      'ddb255570', 
      'mdb650204' 
     ]; 
#comment: this is the Dumper $row result of 2nd run in loop 
$VAR1 = [ 
      'Vehicle Factory', 
      'D3', 
      '2518, 1613, 1512, 1109, 912 bus, 712 bus, 613 Export', 
      ' mgp657301', 
      'ddb255570', 
      'mdb650204' 
     ]; 
#comment: this is Dumper @newRows result of second run in loop 
the new value is inserted but the first value becomes same as new value 
$VAR1 = [ 
      'Vehicle Factory', 
      'D3', 
      '2518, 1613, 1512, 1109, 912 bus, 712 bus, 613 Export', 
      ' mgp657301', 
      'ddb255570', 
      'mdb650204' 
     ]; 
$VAR2 = $VAR1; 
+0

更改了接受的答案,因爲@ aidan's提供了一個更好的方法來執行相同或更簡單的操作。 – norbdum

回答

4

$row參考到數組。您反覆將此參考推入@newRows,因此@newRows最終將持有一組指向同一事物的指針。您需要在每次推入陣列時製作陣列的副本@newRows

例如,

my @arr = (1, 2, 3); 
my $ref_a = \@arr; 
my $ref_b = \@arr; 

$ref_a->[0] = "test"; 
print $ref_b->[0]; # prints "test"; 

p.s.你過於複雜的東西:

my @row = ('Vehicle Factory', 'D3', '2518, ...', ...); 
my @newRows =(); 

for my $k (split /,\s*/, $row[3]) 
{ 
    push @newRows, [@row[0..2], $k, @row[4..$#row]]; 
} 
+1

乾淨的做法+ 1 – Hameed

1
my $row = \@oneRow; 

還有你的問題。 你持有一個參考@oneRow 所以,當你訪問的第4單元從$去參考行要更改@oneRow的值[3]

你也推該引用到列表中

,而不是打印自卸車,只是 打印@newRows

將闡明(你會看到相同的指針在那裏兩次)

1

我不知道你想在最後得到什麼。下面的代碼會給你multidimential陣列

#!/usr/local/bin/perl 


use Data::Dumper; 
use strict; 
my @oneRow = ('Vehicle Factory', 
     'D3', 
     '2518, 1613, 1512, 1109, 912 bus, 712 bus, 613 Export', 
     'ajj137035, mgp657301', 
     'ddb255570', 
     'mdb650204' 
     ); 
my @newRows; 
my $userString = $oneRow[3]; 
my @userNewRow = split(/,/,$userString); 
foreach(@userNewRow) { 
    $oneRow[3] =~ s/.*/$_/; 
    push @newRows, [@oneRow]; 
} 

print Dumper \@newRows; 

輸出:

$VAR1 = [ 
      [ 
      'Vehicle Factory', 
      'D3', 
      '2518, 1613, 1512, 1109, 912 bus, 712 bus, 613 Export', 
      'ajj137035', 
      'ddb255570', 
      'mdb650204' 
      ], 
      [ 
      'Vehicle Factory', 
      'D3', 
      '2518, 1613, 1512, 1109, 912 bus, 712 bus, 613 Export', 
      ' mgp657301', 
      'ddb255570', 
      'mdb650204' 
      ] 
     ]; 

只得到那麼列表推式更改爲:

push @newRows, @oneRow; 

輸出:

$VAR1 = [ 
      'Vehicle Factory', 
      'D3', 
      '2518, 1613, 1512, 1109, 912 bus, 712 bus, 613 Export', 
      'ajj137035', 
      'ddb255570', 
      'mdb650204', 
      'Vehicle Factory', 
      'D3', 
      '2518, 1613, 1512, 1109, 912 bus, 712 bus, 613 Export', 
      ' mgp657301', 
      'ddb255570', 
      'mdb650204' 
     ]; 

我不明白創建一個的原因參考,所以我刪除了這部分。

這有助於您下次發佈您想要的輸出:)。

+0

我添加了引用來複制Text :: CSV讀取csv行的方式。 – norbdum