in reply to Re^3: combining lists along with a regex
in thread combining lists along with a regex

Hmmm. Not meaning to quibble, but I notice your inputs don't have the newlines that mine do, which I don't want in the output, as I want to provide my own as opposed to reproducing whatever I had in the input, which may or may not be a windows-appropriate list, as that is where these get printed off ultimately. I've read (in perlmonks) that s/\s+$//; is the best way to strip newlines and extra spacing on RHS. Without them I get this:

0. Amber B. was harmed by J. 1. Kim B. was harmed by B F K. 2. Kim W. was harmed by A I J.

Now that I look at this, it isn't at all clear to me how I would accomplish that in one step with the greediness of the .*. Indeed, I don't understand why it doesn't match .* and get discarded without the s/\s+$//;:

for (@name) { s/\s+$//; my $int = s/^(\d+\.\s+\w+\s+\w).*$/$1. /; say "int is $int"; } for (@harm) { s/\s+$//; my $int = s/^\d+\.\s+(\w.*)$/was harmed by $1./; say "schmint is $int"; }
0. Amber B. was harmed by J. 1. Kim B. was harmed by B F K. 2. Kim W. was harmed by A I J.

The point about the period not needing to be escaped in the right hand part of the substitution is well-taken and reflected in the above. Thanks again.

Replies are listed 'Best First'.
Re^5: combining lists along with a regex
by Athanasius (Archbishop) on May 10, 2015 at 02:54 UTC

    You’re right, I didn’t take newlines into account. You could always chomp the line before the substitution. However, to do it all in one step, you need to add an /s modifier to the (regex part of the) substitution:

    12:51 >perl -wE "my $s = qq[Hello world. \n]; $s =~ s/^.*(world).*$/m +onks/; say qq[|$s|];" |monks | 12:51 >perl -wE "my $s = qq[Hello world. \n]; $s =~ s/^.*(world).*$/m +onks/s; say qq[|$s|];" |monks| 12:51 >

    See perlre#Modifiers.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,