Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^2: adding an index column to a csv data file

by ng0177 (Acolyte)
on Jun 02, 2019 at 12:55 UTC ( [id://11100844]=note: print w/replies, xml ) Need Help??


in reply to Re: adding an index column to a csv data file
in thread adding an index column to a multi-array derived from a csv data file

my $i=0; $_->[2] = $i++ for @multi_array;

works for me too now after resetting $i which masked it.

You are right, the present data set has two columns and the other one three. I should have mentioned that.

Thanks for helping out. The generalisation is useful to have.

Replies are listed 'Best First'.
Re^3: adding an index column to a csv data file
by AnomalousMonk (Archbishop) on Jun 02, 2019 at 17:08 UTC

    You could generalize this a bit further and eliminate the  $i variable (one less thing to worry about):

    c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my @multi_array = ([ 99, 88 ], [ 77, 66 ], [ 55, 44 ], ); ;; push @{ $multi_array[$_] }, $_ for 0 .. $#multi_array; dd \@multi_array; " [[99, 88, 0], [77, 66, 1], [55, 44, 2]]
    but I'm not sure this is really an improvement WRT clarity and maintainability! unshift or splice could be used to stuff the new element at other offsets in the array. The "pure index" version is even more obscure:
    c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my @multi_array = ([ 99, 88 ], [ 77, 66 ], [ 55, 44 ], ); ;; $multi_array[$_][ @{$multi_array[$_]} ] = $_ for 0 .. $#multi_array; dd \@multi_array; " [[99, 88, 0], [77, 66, 1], [55, 44, 2]]


    Give a man a fish:  <%-{-{-{-<

      The $i less variant is good for learning.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11100844]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2024-04-16 20:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found