GertMT has asked for the wisdom of the Perl Monks concerning the following question:

goodday fellow monks,
sometimes the smallest issues seem to be the biggest hurdles in my coding adventures.

I have a file with data where numbers with decimals are written like  100,25. I want to replace the comma for a dot and multiply numbers but I can't get it working.
#!/usr/bin/perl -w use strict; use warnings; use diagnostics; use Data::Table; my $table = new Data::Table( [ [ "100,20", "18,20", "19,45", "7,25" ], [ "2", "3", "2", "2" ],[ +0,0,0,0] ], [ 'Numbers', 'Values' , 'Result'], 1 ); $table->colsMap( sub { $_->[2] = ($_->[0] =~ s/,/\./g) * $_->[1] } ); print $table->csv;
Thanks for any reply

Replies are listed 'Best First'.
Re: replace comma for dot re. calculation
by GrandFather (Saint) on Jun 22, 2007 at 09:45 UTC

    Bitten by over thinking the plumbing. Change the colsMap line to:

    $table->colsMap( sub {$_->[0] =~ s/,/\./g; $_->[2] = $_->[0] * $_->[1] + } );

    and the code prints:

    Numbers,Values,Result 100.20,2,200.4 18.20,3,54.6 19.45,2,38.9 7.25,2,14.5

    which I suspect is what you were after.

    The result of $_->[0] =~ s/,/\./g is true (1) if there was a match. You were expecting the resulting value of $_->[0] perhaps?


    DWIM is Perl's answer to Gödel
      Multiplying Numbers * Values or column 0 times column 1 is indeed where I was after. I kept getting 1 or nothing as a result. I'm very happy with this, it is indeed where I was after! I understand I have to first do the replace and then the calculation.
      Thanks again.