CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Matching with multiple lines

 



joggi
New User

Feb 14, 2014, 9:15 AM

Post #1 of 4 (1127 views)
Matching with multiple lines Can't Post


Code
$log1 = "Generating an unsorted array took 1.259 seconds\n"; 
$log1 .= "Sorting took 10.486 seconds\n";
$log1 .= "Generating an unsorted array took 1.346 seconds\n";
$log1 .= " Sorting took 9.276 seconds\n";
$count = 0;
$_ = $log1;
while (/^Generating.*Sorting/smg) {
$count ++;
print "3: Match $count found at $-[0] to ",$+[0]-1,"\n"; }
print "Total number of matches found: $count\n\n";


I'm trying to get the start of the line with generating,
so it should have 2 matches, with 2 start and end points for each line where it starts.

I've only been learning it for 2 weeks now, so I'm completely new, any help would be much appreciated.


Laurent_R
Veteran / Moderator

Feb 14, 2014, 10:11 AM

Post #2 of 4 (1122 views)
Re: [joggi] Matching with multiple lines [In reply to] Can't Post

There are many possible ways. One simple may be:


Code
my $count = 0; 
my @linelist = split /\n/, $log1;
while my $one_line (@linelist) {
print "$one_line\n" and count++ if $one_line =~ /Generating/;
}
print "Number of matching lines: $count \n";



BillKSmith
Veteran

Feb 14, 2014, 12:46 PM

Post #3 of 4 (1112 views)
Re: [joggi] Matching with multiple lines [In reply to] Can't Post

Your code would work fine with a "non-greedy" match.

Code
while (/^Generating.*?Sorting/smg) {

Good Luck,
Bill


joggi
New User

Feb 14, 2014, 12:59 PM

Post #4 of 4 (1107 views)
Re: [joggi] Matching with multiple lines [In reply to] Can't Post

Thanks bill, that worked

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives