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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.