Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Change decimal point temporarily

by lima1 (Curate)
on Aug 02, 2010 at 20:50 UTC ( [id://852538]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks,

I have some trouble changing the decimal point from "." to "," temporarily for parsing a CSV file. It works with:

setlocale LC_NUMERIC, "fr_FR"; #parse .... #revert setlocale LC_NUMERIC, "";
But this requires that "fr_FR" is installed. The localconf() function returns a nice hash, but there seems to be no counterpart for setting the locale with such a hash.

What do you recommend?

Update: Sorry, I was very unclear. Please assume I want to load a CSV (tab or semicolon separated) file and I know that the decimal point is comma. Such files are unfortunately quite common in European countries. The above code seems to work in the way that numerical columns are numeric in Perl after parsing. But it seems like a hack to hard code a temporary locale which uses a comma as decimal point.

Update 2: Problem seems to be solved Re^7: Change decimal point temporarily. There seems to be no way to avoid a regex on all fields. But can anybody explain what's happening internally here Re^6: Change decimal point temporarily? I don't quite get it... Seems more like a bug than a feature to me.

Replies are listed 'Best First'.
Re: Change decimal point temporarily
by ikegami (Patriarch) on Aug 02, 2010 at 20:52 UTC
      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.
        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.

        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?
Re: Change decimal point temporarily
by nvivek (Vicar) on Aug 03, 2010 at 04:51 UTC

    You can use the substitute command to do this.If you want to replace the . for only numerals.Before replacing,you can check whether that variable has numerals or not by using regular expression in substitute command as follows.

    $no=123.45; $no=~s/^(\d+)\.(\d+)/$1,$2/;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-04-16 14:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found