Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^2: Change decimal point temporarily

by lima1 (Curate)
on Aug 02, 2010 at 21:01 UTC ( [id://852544]=note: print w/replies, xml ) Need Help??


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

I was unclear. It should work automatically. With a regex, I first have to test whether it is a numeric field. Obviously, I don't want to replace points in text columns.

Replies are listed 'Best First'.
Re^3: Change decimal point temporarily
by ikegami (Patriarch) on Aug 02, 2010 at 22:35 UTC
    use Scalar::Util qw( looks_like_number ); my $s = 123.45; $s =~ s/\./,/ if looks_like_number($s);

    Works even better than setlocale because you are surely starting with the number in string form if you don't know whether a column contains a number or not.

      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.

      Jenda
      Enoch was right!
      Enjoy the last years of Rome.

        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
Re^3: Change decimal point temporarily
by fod (Friar) on Aug 02, 2010 at 22:53 UTC
    Or... I suppose if we have a digit either size of a '.' then it's probably a number with a decimal point:
    $s = "123.45"; $s =~ s/(?<=\d)\.(?=\d)/,/;
    ALthough... I don't quite understand what you're trying to do - surely if you're parsing a CSV file containing numbers with decimal points you'd rather they weren't commas?
      The motivation is a feature for the module described in RFC: Text::CSV::R. It is an option to set the decimal point. In Germany for example, it is common for CSV files to use a comma as decimal point and a semicolon as field separator.

      So I guess a locale approach would be the performance-wise fastest solution. But setting the locale to an arbitrary locale which uses a comma (de_DE, fr_Fr,...) just to change this single parameter seems like hack.

        I consider using a comma instead of a decimal like that to be a hack in and of itself ... so, when in Rome. ;)

        jeffa

        L-LL-L--L-LL-L--L-LL-L--
        -R--R-RR-R--R-RR-R--R-RR
        B--B--B--B--B--B--B--B--
        H---H---H---H---H---H---
        (the triplet paradiddle with high-hat)
        

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (6)
As of 2024-03-28 20:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found