Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re^4: Change decimal point temporarily

by Jenda (Abbot)
on Aug 02, 2010 at 23:05 UTC ( #852564=note: print w/replies, xml ) Need Help??

in reply to Re^3: Change decimal point temporarily
in thread Change decimal point temporarily

use Scalar::Util qw( looks_like_number ); print looks_like_number('12,48'); # --> 0
He's PARSING a csv (or more probably a tab or semicolon separated file) so he needs to go exactly the other way around. Accept 12,48 and treat it as a number.

Enoch was right!
Enjoy the last years of Rome.

Replies are listed 'Best First'.
Re^5: Change decimal point temporarily
by ikegami (Patriarch) on Aug 02, 2010 at 23:35 UTC

    I don't think so. That's the opposite of what he said (changing the decimal point from "." to ","), and it's the opposite of what his code does.

    I see what you mean, but it still make no sense since he says setlocale works.

    $ perl -we'use POSIX; setlocale LC_NUMERIC, "fr_FR"; print 0+"123,45"' Argument "123,45" isn't numeric in addition (+) at -e line 1. 123
      Hm, It seems to work if you use locale:
      perl -we 'use POSIX; use locale; setlocale LC_NUMERIC, "fr_FR"; $x = " +123,45"; $x+=0.1; print $x; setlocale LC_NUMERIC, ""; $x += 0.1; prin +t $x;'
      Update: this does not work:
      perl -we 'use POSIX; use locale; setlocale LC_NUMERIC, "fr_FR"; $x = " +123,45"; print $x; setlocale LC_NUMERIC, ""; $x += 0.1; print $x;'
      So what I do now is to add 0 if it looks_like_number() and everything works fine. At least what I've tested until now...

        ah! In that case,

        (my $tmp = $s) =~ s/,/./; $s = $tmp if looks_like_numbers($tmp);

        You'd be better of specifying which columns are numeric, though. Text::CSV_XS can even take advantage of that information if you tell it (via types).

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2022-01-28 06:39 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (73 votes). Check out past polls.