Hi, Someone on the gtk2 perl maillist was asking a question about number text returned from a label, being in European and commafied format, and not acting as a number,

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";

I'm not really a human, but I play one on earth. Cogito ergo sum a bum

In reply to disappearing digit with regex and tr by zentara

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.