in reply to Extracting up to a certain string

my @foo; { local $/ = 'certain string'; open my $file, '<', '/path/to/afile' or die $!; @foo = (<$file>)[0..2]; }

Now @foo contains everything up to and including the third occurance.

After Compline,
Zaxo

Replies are listed 'Best First'.
Re: Re: Extracting up to a certain string
by sauoq (Abbot) on Aug 04, 2002 at 08:35 UTC
    Setting $/ to the string you want won't work if the "P" in </p> might be either upper or lowercase in your input. One way to handle that case is:
    open(FILE, "yourfile") or die; my $file = do {local $/; <FILE>}; $file =~ m!(.*?</p>.*?</p>.*?</p>)!is; my $match = $1;
    -sauoq
    "My two cents aren't worth a dime.";