zentara has asked for the wisdom of the Perl Monks concerning the following question:
Well I thought, this ought to be easy with a tr or regex, but it isn't. So before I blow my mind on Friday, I figure I would ask about this.
The code below, takes a float, converts it to European format, then I attempt, to tr ( or regex ) the comma's out. I keep losing the digit 7. ???? I can't make any sense why, although I did find a clunky way (shown below).
Can the regex experts explain what is happening?
#!/usr/bin/perl my $num_in = 1234567.89; print "$num_in\n"; my $num_out = scalar reverse join "", map { s/(\d{2})\./$1,\b/; s/^(\d{1,3})$/\.$1/; + $_ } (reverse sprintf("%.2f", $num_in)) =~ m/.{1,3}/g; print "$num_out\n"; my $get_text = $num_out; print "$get_text\n"; #this loses the 7 # $get_text =~ s/\.//g; # tr is similar $get_text =~ tr/.//d; #this clunky method works to remove commas # $get_text =~ s/\./ /g; # # $get_text =~ s/\s+//g; #won't work to remove spaces # $get_text =~ s/\s+//; # $get_text =~ s/\s+//; #this then works to change decimal point to . $get_text =~ tr/,/./d; # now if I try to force numeric context it fails #$get_text = $get_text + 0.00; print "$get_text\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: disappearing digit with regex and tr
by ikegami (Patriarch) on Aug 18, 2006 at 15:04 UTC | |
|
Re: disappearing digit with regex and tr
by Velaki (Chaplain) on Aug 18, 2006 at 15:13 UTC | |
|
Re: disappearing digit with regex and tr
by duff (Parson) on Aug 18, 2006 at 15:06 UTC | |
|
Re: disappearing digit with regex and tr
by zentara (Cardinal) on Aug 18, 2006 at 15:45 UTC | |
|
Golf, anyone? :)
by suaveant (Parson) on Aug 18, 2006 at 17:40 UTC | |
by Sidhekin (Priest) on Aug 18, 2006 at 17:56 UTC | |
by suaveant (Parson) on Aug 18, 2006 at 18:02 UTC |