P is for Practical PerlMonks

### Re^3: Change decimal point temporarily

by ikegami (Pope)
 on Aug 02, 2010 at 22:35 UTC ( #852562=note: print w/replies, xml ) Need Help??

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

```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.

Replies are listed 'Best First'.
Re^4: Change decimal point temporarily
by Jenda (Abbot) on Aug 02, 2010 at 23:05 UTC
```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
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...

Create A New User
Node Status?
node history
Node Type: note [id://852562]
help
Chatterbox?
 [RonW]: Tunnel FTP through stunnel? [james28909]: i guess i should have done it in perl. i bet it woulnt have taken me 5 hours to figure out haha [choroba]: In the end, I was able to upgrade Perl from 5.8.3 to 5.22 and install Net::SFTP::Foreign , at least for the task involved [james28909]: what would be the best way to capture that stream with perl? ffmpeg args -rtp rtp://127.0.0.1? [LanX]: if I was forced to talk about all security risks I encountered oO [james28909]: if i did that with ffmpeg, i could then listen on the port with perl right?

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (11)
As of 2017-05-22 21:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
My favorite model of computation is ...

Results (174 votes). Check out past polls.