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.

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: 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!

In reply to Re: Newbie Question on parsing files by saintly
in thread Newbie Question on parsing files by jping45

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.