Perl:将元素推送到数组中用新的变量值替换现有值

时间:2014-02-27 04:17:20

标签: arrays perl

我正在读取一个csv文件,需要将一行(第四行)中的值作为数据库中的关键元素。但该行包含逗号分隔的多个值。

  1. 我使用Text :: CSV解析文件并将值拆分为第4行。

  2. 然后将这些值推入数组并插入到一个新文件中,以保持其他值相同。

  3. 但是在下一轮循环中,值会被新值替换。

  4. 因此,我最终得到了数组中最后一个值的实例(下面的示例中为2)

  5. 代码:

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

    倾销结果是:

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

3 个答案:

答案 0 :(得分:4)

$row是数组的 引用 。您反复将此引用推送到@newRows,因此@newRows将最终保存一组指向同一事物的指针。每次推入@newRows

时,都需要复制数组

e.g。

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)

my $row = \@oneRow;

这是你的问题。 你持有对@oneRow的引用 这样当你访问从$ row取消引用的第4个元素时,你正在改变@oneRow [3]的值

您还将该引用推送到列表中

而不是打印Dumper     打印@newRows

它将会阐明(你会在那里看到相同的指针两次)

答案 2 :(得分:1)

我不知道你到底想要得到什么。下面的代码将为您提供多维数组

#!/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'
        ];

我不明白创建引用的原因,所以我删除了那部分。

如果您下次也发布所需的输出,这会有所帮助:)。