Congratulations on choosing Perl, the One True Language!
There are a couple problems with the code you've written that might prevent it from working correctly.
$file = <FILE>;
This would only get one line from the file (when you ask for a scalar value (as opposed to a list) from a filehandle, you get back only one line by default). You might modify this to:
local $/ = undef;
$file = <FILE>;
The '$/' special variable is normally set to '\n', telling Perl to stop reading a file when it hits a newline. By setting it to 'undef', you force Perl to give you the whole file.
You could just as easily say:
$file = join("", <FILE>);
too.
The foreach command sets a single variable to each element of the thing it's 'each'ing through. Since you didn't specify it, it's a bit hidden right now. (It's using the special variable $_). You could make it more clear:
foreach $this_paragraph (@paras) {
....
}
And then check the value of $this_paragraph to see if it matches your criteria. As an example:
foreach my $this_paragraph (@paras) {
my @split_para = split(/:/, $this_paragraph);
if( $split_para[1] = $KWD ) {
print $this_paragraph;
}
}
However, this still has some problems. From your description it seems as if you want to search for a keyword followed by a ':', as in 'KEYWORD: some text in the paragraph'. Even if you're searching for ':KEYWORD', this code still has some problems.
- $split_para[1] = $KWD; # this SETS $split_para[1] to $KWD, and always evaluates to TRUE as long as $KWD was true. You probably want something like '$split_para[1] eq $KWD' instead.
- If the paragraph doesn't look like 'something:KEYWORD:something....', this test will fail. Perl's lists/arrays start counting at 0 instead of 1, so the first list element is '$split_para[0]'. You could test the first one with '$split_para[0] eq $KWD' if your intended text looks like 'KEYWORD:something ....'
- It's not really necessary to break up the paragraph into pieces to check if it's got the keyword in it, Perl programmers are very fond of using regular expressions for this sort of thing...
foreach my $this_paragraph (@paras) {
if( $this_paragraph =~ /^$KWD:/s ) {
print $this_paragraph;
# This works if the paragraph looks like 'KEYWORD: ...'
}
}
If your paragraph looks more like 'something ..... :KEYWORD' instead, then the regular expression needs a little modification:
foreach my $this_paragraph (@paras) {
if( $this_paragraph =~ /:$KWD\b/s ) {
print $this_paragraph;
# This works if the paragraph looks like '.... :KEYWORD ...'
}
}
update:
The others made some good points about rewriting the way you open files. The way you chose would work, but if you get in the habit of doing it that way, it's prone to problems.
Here are some resources that can help you:
- perlretut - Regular Expressions tutorial
- open - Syntax for the 'open' command. Although the examples in the Learning Perl book demonstrate it, there are 'safer' ways of opening files. As the other monks have pointed out, you should usually use scalar filehandles (open my $fh, ....) instead of the older (open FILE, ...), and you should always check the return code from 'open' to make sure it worked.
- http://www.oreilly.com/catalog/perlbp/ - O'Reilly's 'Perl Best Practices' book. Although this doesn't teach you how to write Perl programs, it's a great book new Perl programmers should read if you plan to write Perl code professionally.
- http://www.oreilly.com/catalog/pperl3/ - The companion book to 'Learning Perl', 'Programming Perl' is a reference to the built-in Perl commands and the tools and libraries that come with it.
Although I probably shouldn't mention this, all the
cool people write out the name of our beloved language as 'Perl', not 'PERL'.
Some of the other examples don't seem to be looking at paragraphs (such as the first, which only prints lines).
Good luck with Perl, and I hope the responses here haven't been too overwhelming! I would suggest experimenting by seeing what happens when you make use different alternatives for each line of the program. And of course, I'm sure we all hope you'll come back and ask more questions if you need more help!