in reply to Matching ranges

    Once again, I just want what is in between first occurrence of "Services" and "Users".

I'm assuming that you aren't interested in the lines with the "======" stuff. Based on that, here's how I would approach the problem.

use strict; my $file; open(DATA,"<data.txt") || die "Unable to open file 'data.txt': $!\n"; { local $/; $file = <DATA>; } close(DATA); my ($selection) = ($file =~ m/^.+?Services.+?[=]+(.+?)[=]+/is); my (@lines) = split /\n/,$selection; for (my $i=0;$i<=$#lines;$i++) { $lines[$i] =~ s/\n//; print "Line $i: $lines[$i]\n"; }

Which produces the following output:

Line 0: Line 1: blah Line 2: glah Line 3: sfsd Line 4: Line 5: Line 6: asfsdf Line 7: afsafdf

Although BrowserUK's code is probably better, the code above may be easier to follow for some folks (such as myself). Also, I should note that I do lose a blank line at the end of the captured section when I use the split command. Again, I'm assuming that won't create problems for what you're trying to do.

Anyways, this gives you an example of another approach to solving the problem.

Replies are listed 'Best First'.
Re^2: Matching ranges
by ldbpm (Initiate) on Sep 02, 2010 at 12:15 UTC

    I like your output, but there is a lot of code to produce that output, and I am trying to produce concise code. This particular code is definitely an option to consider, though.

    Thanks