"Jeff 'japhy' Pinyan" <[EMAIL PROTECTED]> writes:
> On Sep 6, Harry Putnam said:
>
>>IMPORTANT: I don't want techniques involving call back (remembered)
>>operators and parens, I know how to piece those together for simple
>>things like the file below.
>
> Is there a reason for that limitation? Oh well. Anyway, here's a good
> approach:
Only a dumb or misinformed one.. I had assumed it would be a complex
thing looking like someone barfed alphabet soup with match sticks in
it, and lots of parens. ... hehe.
It appears that using perl defaults as some of the suggestions do its
really pretty simple.
>
> while (<INPUT>) {
> print substr($_, $-[0], $+[0] - $-[0]), "\n" if /pattern/;
> }
> For information about what the @- and @+ arrays represent, read 'perlvar'.
>
> This is more or less the same as
>
> while (<INPUT>) {
> print "$&\n" if /pattern/;
> }
I haven't read the suggested material yet, having just seen your
post, but I noticed the post using the $& formulation you quote
above seemed to lack the ability to register multiple hits on a
single line as the /(pattern)/g formulation is able to do.
Ditto with your formulation above.
cat test.f
here is a string and another string
only one string here
again only one sting
now go with 3 strings with string matter strung between the strings
strings
cat $& style script test.pl
#!/usr/local/bin/perl -w
while (<>) {
print "$&\n" if /string\w*/;
}
Result:
$ ./test.pl test.f
string
string
strings
strings
=======
cat Jeff's formulation test3.pl
#!/usr/local/bin/perl -w
while (<>) {
print substr($_, $-[0], $+[0] - $-[0]), "\n" if /string\w*/g;
}
Results:
$ ./test3.pl test.f
string
string
strings
strings
======
Whereas the /(pattern)/g formula gets them all.
cat test2.pl
#!/usr/local/bin/perl -w
while (<>) {
push @array, $_ =~ /(string\w*)/g;
}
for(@array){
print "$_\n";
}
Results:
$ ./test2.pl test.f
string
string
string
strings
string
strings
strings
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]