Re: print only what a regex actually hits

2002-09-08 Thread Harry Putnam
"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?

Re: print only what a regex actually hits

2002-09-08 Thread Jeff 'japhy' Pinyan
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: while () {

Re: print only what a regex actually hits

2002-09-06 Thread George Schlossnagle
Oops - teaches me not to test my solution :). Both suffer from not adding a newline to the last element of the join. You could stick a newline at the end, but doing so naively will generate a number of empty lines. The simplest thing is probably: while(<>) { print join("", map { "$_ l

Re: print only what a regex actually hits

2002-09-06 Thread Harry Putnam
George Schlossnagle <[EMAIL PROTECTED]> writes: > while(<>) { > print join("\n", map { "$_ lineno $." } /(string)/g); > } George, I haven't gotten good results with either of the pieces of code you posted. This one gives me. string lineno 1 string lineno 1string lineno 2 I can fix

Re: print only what a regex actually hits

2002-09-06 Thread George Schlossnagle
while(<>) { print join("\n", map { "$_ lineno $." } /(string)/g); } On Friday, September 6, 2002, at 10:32 PM, Harry Putnam wrote: > Tinkering with some of the suggestions here, I was looking for a way > to get the line number in there. Thought maybe I could just > con

Re: print only what a regex actually hits

2002-09-06 Thread Harry Putnam
Tinkering with some of the suggestions here, I was looking for a way to get the line number in there. Thought maybe I could just concatenate it in there: With this test file: string strung strang string other junk blabbitty string other junk while(<>){ push @array,$_ =~ /(string)/g . "

Re: print only what a regex actually hits

2002-09-06 Thread Harry Putnam
Timothy Johnson <[EMAIL PROTECTED]> writes: > You mean something like this? > > while(){ > push @matches,$_ =~ /(silly)/; > } > > foreach(@matches){ > print $_."\n"; > } Bingo... but this looks like it might be a sort of default callback or remembered item inside parens. Is it? Anyway

Re: print only what a regex actually hits

2002-09-06 Thread Harry Putnam
david <[EMAIL PROTECTED]> writes: > you are probably looking for the $& variable: > > open(FILE,'file') || die $!; > while(){ > print $&,"\n" if(/silly/); > } > close(FILE); > cool, a whole different way to do it thanks -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional co

Re: print only what a regex actually hits

2002-09-06 Thread George Schlossnagle
or while() { /(myregex)/ && print "$1\n"; } or if you are concerned about multiple matches in a line while() { print join("\n", /(myregex)/g); } if there are worries about Timothy Johnson wrote: >You mean something like this? > >while(){ > push @matches,$_ =~ /(silly)/; >} > >foreac

Re: print only what a regex actually hits

2002-09-06 Thread david
you are probably looking for the $& variable: open(FILE,'file') || die $!; while(){ print $&,"\n" if(/silly/); } close(FILE); __END__ you probably want to check out $`(pre match) and $'(post match) as well. note the $&, $` and $' are kind of expensive and you usually can do the above w

RE: print only what a regex actually hits

2002-09-06 Thread Timothy Johnson
You mean something like this? while(){ push @matches,$_ =~ /(silly)/; } foreach(@matches){ print $_."\n"; } -Original Message- From: Harry Putnam [mailto:[EMAIL PROTECTED]] Sent: Friday, September 06, 2002 4:18 PM To: [EMAIL PROTECTED] Subject: print only what a regex actuall