pkill pkill - 2 years ago 98
Perl Question

Perl read text in paragraph and patternmatch

I have an index file, which keeps an index of each object separated by a blank line. Now, I have to search for a keyword on each object, and if present, dump to another file, instead of rebuilding the entire index from the scratch. The piece of code

###@files is an array that contains the list of packages in the index
open("FH", $indexfile) or die ;
my @linearray = <FH>;
close ("FH");

open (NFH, '>', "$tmpfile") or die "cannot create";
foreach my $pattern (@files)
if (my @matches = grep /$pattern/, @linearray) {
print NFH "@matches";
} else {
push @newpkgs,$pattern;
close (NFH);

But this is not working as expected. How can I get a paragraph as an element in an array?

Answer Source

Modify $/

my @linearray;
    open("FH", $indexfile) or die ;
    local $/ = '';
    @linearray = <FH>;
    close ("FH");
    print Dumper @linearray;

This will give you the required output.

I used the braces {} to limit the scope of Field Record Separator modification till the objects are read to the array. You can extend it according to your requirement.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download