Hi ng0177,

First please note that the sprintf builtin does not print out anything: it only returns a formatted string. In your code, the call to sprintf does not do anything useful, since you're not using the return value. You need to either use print to print out the string produced by sprintf, or use printf.

Second, it seems that you're using sort with the aim to modify the values in your array of arrays, rather than for sorting purposes. That's not really the right tool for that. Use a for loop or a map statement (depending on whether you want to modify your data structure in place or create a new data structure).

Perhaps that's more or less what you looking for:

use strict; use warnings; my @multi_array = map [split], <DATA>; my @by_second = map {$_->[1] *= 1.1E3; $_;} @multi_array; printf "%E %E %E\n", @{$_}[0..2] for @by_second; __DATA__ 1.10000E0 1.00000E0 1.00000E0 2.20000E0 2.00000E0 2.00000E0 3.30000E0 3.00000E0 3.00000E0
which produces the following output (which is presumably what you're looking for):
$ perl split.pl 1.100000E+00 1.100000E+03 1.000000E+00 2.200000E+00 2.200000E+03 2.000000E+00 3.300000E+00 3.300000E+03 3.000000E+00
If you insist on reducing the number of code lines, you could do it directly (without the auxiliary arrays) like this:
use strict; use warnings; printf "%E %E %E\n", @{$_}[0..2] for map {$_->[1] *= 1.1E3; $_;} map [ +split], <DATA>;
or possibly with only one map statement (but the code doesn't get shorter):
printf "%E %E %E\n", @{$_}[0..2] for map {my $c = [split]; $c->[1] *= +1.1E3; $c;} <DATA>;
I kept relatively close to your code to try to explain some of the problems in your script and show possible ways to solve them, but, as pointed out by choroba, using Text::CSV might be a good idea.

In reply to Re: multiplication a column of data in csv file by a factor by Laurent_R
in thread multiplication a column of data in csv file by a factor by ng0177

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.