monks,

Using a foreach loop works fine, but when I try to put in a nested "if" statement the program doesn't seem to work.

I'm trying to screen throught the contents of a webpage to determine if a company is listed. If it is listed, I want to pull out the information starting at the company name ($company) until the tag </tr>.

I need to determine if the company is listed before doing the extraction.

The basic code without determining if the company is listed or not before extracting works fine.

This works ...

$content = get($url); @companies = ("AOL Time Warner", "Genetech","Broadwing"); foreach $company (@companies){ $content =~ /$company(.*?)<\/tr>/gsmi; $new_coverage = $1; print "$new_coverage <p>" }
BUT when I try to put the if statement within the foreach loop, it doesn't seem to work.

This doesn't work ...

$content = get($url); @companies = ("AOL Time Warner", "Genetech","Broadwing"); foreach $company (@companies){ if ($content =~ /$company/gsmi){ $content =~ /$company(.*?)<\/tr>/gsmi; $new_coverage = $1; print "$new_coverage <p>"; } }

Any idea's on why this is happening?

thanks


In reply to foreach loop with nested by cdherold

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.