Perl regular expression guidance

wildwolf

Golden Member
Jan 9, 2000
1,690
0
76
Hi all.

I'm trying to whip up a perl script...and it's been a while since I've messed with perl.

I've got a line of text stored in a variable, aptly named $line and I've got a pattern I've defined called $pattern.

I'd like to see if I can find $pattern in the $line.

If my pattern were simple text, such as "abcd" I'd do something like:

if ( $line =~ m/abcd )
{
print "Match found, line contains abcd.";
}

However, I'd like to know if it's possible in Perl to do this:

if ( $line =~ m/$pattern )
{
print "Match found, line contains abcd.";
}

Would there be any constraints if my $pattern had characters I would normally escape with the \?

*edit* sorry about the code...tried to do the attach code..but it doesn't show up in preview...so thought it was lost. No idea how to "remove" the attached code block either.
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
You can stick patterns in variables exactly as if you copied and pasted them out of m/pattern/.

Example:
 

wildwolf

Golden Member
Jan 9, 2000
1,690
0
76
Yeah. I think I realized where my problem really stems from.

I got that working...which lead me to find the root of my problem: I am trying to open a file read each line, do some processing on it, and proceed to the next line.

How do I proceed to the next line if, during any part of processing the current line, I wish to stop with that line and just go to the next one?

I opened the file with a standard open(myFile, "<filename".
I then used a (see code).


In the (get next line here)....how do I accomplish this...cause what I'm attempting isn't working. I thought my regex pattern was failing...it wasn't. It matched, just never got the to the next line to search for the string again.
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
the perl "next" keyword will stop the current iteration of a loop and go directly to the next one.

while($something){
# do stuff
next;
# stuff that doesn't get done because "next" forced the next iteration of the loop without running it.
}
 

jgbishop

Senior member
May 29, 2003
521
0
0
I believe that a call to <myFile>, in the (get next line here) section, will do what you want. So, in your while loop:

$line = <myFile>;
print "$line\n";
etc...
 

wildwolf

Golden Member
Jan 9, 2000
1,690
0
76
Originally posted by: jgbishop
I believe that a call to <myFile>, in the (get next line here) section, will do what you want. So, in your while loop:

$line = <myFile>;
print "$line\n";
etc...

That's what I needed/used. Works great. Thanks. Man...I really enjoy Perl....got an awesome parsing tool with less than 40 lines of code.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |