On Tue, March 25, 2008 3:06 am, Yossi Itzkovich wrote:
> Hi,
>
>
> What's the best/simple way to find all lines between startPattern and
> StopPattern:
>
>
> @text= ( "ahfdj\n",
> "ksjdf\n",
> "startPattern\n",
> "gjdhgf\n",
> "kljsd\n",
> "stopPattern\n",
> "jsadflj\n");
>
>
> I would like to get a list of those 2 items in the middle
It's easy to get the range *including* the start and stop elements,
and then pull them out.
@text= ( "ahfdj\n",
"ksjdf\n",
"startPattern\n",
"gjdhgf\n",
"kljsd\n",
"stopPattern\n",
"jsadflj\n");
@text_range = grep { /startPattern/ .. /stopPattern/ } @text;
print for @text_range[1..($#text_range-1)];
# or pop(@text_range); unshift(@text_range); print for @text_range;
It's kind of obscure doing it in one go:
print for grep { (/startPattern/../stopPattern/)=~/(?:[2-9]|\d\d)\z/ }
@text
Both of those assume startPattern won't appear again after stopPattern.
A more brute-force kind of way:
@text_range = @text; # copy if original array shouldn't be modified
while ((unshift(@text_range) // last) !~ /startPattern/) {}
while ((pop(@text_range) // last) !~ /stopPattern) {}
_______________________________________________
Perl mailing list
[email protected]
http://perl.org.il/mailman/listinfo/perl