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

I suggest the below code as the consolidated answer to my question, if there are no objections. The handling of blanks is redundant but could be modified for other purposes.
#!/usr/bin/perl -w my @AoA = (); # ArrayOfArrays # read data plus add index while (<DATA>){ s/\s*//g; my @tmp = split ',',$_; $tmp[0] *= 1.0; $tmp[1] /= 1.0; $tmp[2] = $.; push @AoA,\@tmp; } # or #push @AoA, [split(/,\s*/, $_)] for <DATA>; #push @{ $AoA[$_] }, $_ for 0 .. $#AoA; # modify columns foreach my $row (@AoA) { foreach my $item (@$row) { $item =~ s/^\s+//; } $row->[0] *= 1.0; $row->[1] /= 1.0; } # or #($_->[0] *= 1.0, $_->[1] /= 1.0) for @AoA; # screen output printf "%e,%e,%d\n",@$_ for @AoA; __DATA__ 0.00000000e+00, 2.41644835e+00 1.20048018e-04, 2.38938189e+00 2.40096037e-04, 2.36473989e+00

Replies are listed 'Best First'.
Re^3: adding an index column to a csv data file
by swl (Prior) on Jun 05, 2019 at 02:40 UTC

    No objections from me. There will always be ways of rewriting code, but expressiveness is one of the advantages of Perl.