in reply to Regex match on implicit default variable ($_) in a script, not a one liner

Like this?:

#!/usr/bin/perl use strict; use warnings; use diagnostics; my $A = "abc\ndef\nghi\n"; ( my $B = $A ) =~ s/def\n//g; print "$B"; __END__ C:\test>1145825.pl abc ghi

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
In the absence of evidence, opinion is indistinguishable from prejudice.
  • Comment on Re: Regex match on implicit default variable ($_) in a script, not a one liner
  • Download Code

Replies are listed 'Best First'.
Re^2: Regex match on implicit default variable ($_) in a script, not a one liner
by nikosv (Deacon) on Oct 24, 2015 at 13:43 UTC
    or more compactly using the /r modifier : update: ($B is not needed)
    $A =~ s/def\n//gr; print $A;
Re^2: Regex match on implicit default variable ($_) in a script, not a one liner
by Todd Chester (Scribe) on Oct 24, 2015 at 05:38 UTC

    Thank you, but unfortunately no.
    The "def" will only be a part of the line. If "def" occurs anywhere in the line I want it included.

    #!/usr/bin/perl # full script the following one liner: # $ echo -e "abc\ndef\nghi\n" | perl -wlne '! /def/ and print "$_";' # abc # ghi use strict; use warnings; use diagnostics; my $B = ""; my $A = "abcdef fhijkl mnopqr "; while ( $A =~ m{(.+)}g ) { $B .= "$1\n" unless $1 =~ /ijk/; } print "\$B = \n$B\n"; $B = ""; while ( $A =~ m{(.+)}g ) { $B .= "$1\n" while $1 =~ /ijk/; } print "\$B = \n$B\n";

    $B =
    abcdef
    mnopqr
    Use of uninitialized value $1 in concatenation (.) or string at ./ExtractTest.pl line 26 (#1)

    Isn't "while" suppose to be the opposite of "unless"?

      Isn't "while" suppose to be the opposite of "unless"?

      No it isn't. "while" is the opposite of "until". The opposite of "unless" is "if". All of this is in the fine documentation of Compound Statements.

      The "def" will only be a part of the line. If "def" occurs anywhere in the line I want it included.

      This perhaps?:

      #!/usr/bin/perl use strict; use warnings; use diagnostics; my $A = "abcdef fhijkl mnopqr redefine "; ( my $B = $A ) =~ s[(^.+\n)][ my $x = $1; $x !~ /def/ ? '' : $x ]gme; print "$B"; __END__ C:\test>1145825.pl abcdef redefine

      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
      In the absence of evidence, opinion is indistinguishable from prejudice.