in reply to substr on $_

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

Replies are listed 'Best First'.
Re: (crazyinsomniac: shock) Re: substr on $_
by danger (Priest) on Sep 28, 2001 at 14:08 UTC

    Well, in order to appease crazyinsomniac's shock at the lack of substr() solutions, here's a fun little version:

    #!/usr/bin/perl -w use strict; $_ = 'x' x 11; my($x,$y) = \(substr($_,2,2), substr($_,5)); while(<DATA>){ print $$x,$$y; } __END__ 1996.40637 1996.41064 1996.41199 1996.41467 1996.41882