I am shocked and amazed to hear such blasphemy.

The title of the node is "substr on $_ by gisrob" yet all I see is variations of $new = s/\d+.\d/replacement/g;.

Talk about a shared mentality, didn't anyone notice the word substr?? No, it's not just gibberish, it is an actual perl function.

Observe:

#!/usr/bin/perl -w use strict; while(<DATA>) { my $dot_ix = index $_ ,'.'; printf "%40s: %s\n", "the string before trimming", $_; printf "%40s: %s\n", "dot+", substr $_, $dot_ix; printf "%40s: %s\n", "dot-2", substr($_, $dot_ix - 2); printf "%40s: %s\n", "trimmed fat", substr($_, $dot_ix - 2, $dot_ix -1, ''); # $dot_ix -1 since it's the num of chars, not index printf "%40s: %s\n", "the string after trimming", $_; } __END__ 1996.40637 1996.41064 1996.41199 1996.41467 1996.41882
And the output:
F:\dev>perl sub.pl
              the string before trimming: 1996.40637

                                    dot+: .40637

                                   dot-2: 96.40637

                             trimmed fat: 96.
               the string after trimming: 1940637

              the string before trimming: 1996.41064

                                    dot+: .41064

                                   dot-2: 96.41064

                             trimmed fat: 96.
               the string after trimming: 1941064

              the string before trimming: 1996.41199

                                    dot+: .41199

                                   dot-2: 96.41199

                             trimmed fat: 96.
               the string after trimming: 1941199

              the string before trimming: 1996.41467

                                    dot+: .41467

                                   dot-2: 96.41467

                             trimmed fat: 96.
               the string after trimming: 1941467

              the string before trimming: 1996.41882
                                    dot+: .41882
                                   dot-2: 96.41882
                             trimmed fat: 96.
               the string after trimming: 1941882
UPDATE: OOps, I think funny ;D
#!/usr/bin/perl -w use strict; while(<DATA>) { my $dot_ix = index $_ ,'.'; printf "%40s: %s\n", "the string before trimming", $_; printf "%40s: %s\n", "dot+", substr $_, $dot_ix; # oops, i think funny # printf "%40s: %s\n", # "dot-2", # substr($_, $dot_ix - 2); printf "%40s: %s\n", "string - 2", substr($_, 2); # printf "%40s: %s\n", # "trimmed fat", # substr($_, $dot_ix - 2, $dot_ix -1, ''); # # $dot_ix -1 since it's the num of chars, not index printf "%40s: %s\n", "trimmed fat", substr($_, $dot_ix, 1, ''); printf "%40s: %s\n", "trimmed fat", substr($_, 0, 2, ''); printf "%40s: %s\n", "the string after trimming", $_; } __END__ 1996.40637 1996.41064 1996.41199 1996.41467 1996.41882

 
___crazyinsomniac_______________________________________
Disclaimer: Don't blame. It came from inside the void

perl -e "$q=$_;map({chr unpack qq;H*;,$_}split(q;;,q*H*));print;$q/$q;"


In reply to (crazyinsomniac: shock) Re: substr on $_ by crazyinsomniac
in thread substr on $_ by gisrob

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.