Perl: pushing elements into an array replaces an existing value with a new variable value

I am reading a csv file and should do the values ​​in the line (fourth line) as key elements in the database. But the string contains several values, separated by a comma.

  • I parse the Text :: CSV file and split the values ​​in the 4th line.

  • Then click these values ​​in the array and paste into the new file, saving the other values.

  • But in the next run of the loop, the value is replaced with the new value.

  • Therefore, I get so many instances of the last value in the array (2 in the example below)

The code:

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

Dump Truck Results:

#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;
+4
source share
3 answers

$row - array . @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]];
}
+4
my $row = \@oneRow;

. @oneRow , 4- , $row, @oneRow [3]

Dumper,   print @newRows

( )

+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 :

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

, .

, :).

+1

Source: https://habr.com/ru/post/1529124/


All Articles