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";
In reply to disappearing digit with regex and tr by zentara
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |