cdherold has asked for the wisdom of the Perl Monks concerning the following question:

Monks,

I'm a relative newbie and I'm in a bind. I can't seem to get this simple foreach loop with nested if statement to work. I e-mailed the monks a couple weeks ago and thought i had an answer but i guess i was wrong because it's not working now. Why does this work...

@companies = ("FOO,BAR"); foreach $company(@companies) { print "$company" }

printing out: FOO, BAR.

but if i want to determine if either is present in webpage before printing out by using a nested if statement...

@companies = ("FOO,BAR"); $url = "http://www.yadyadyada.com"; $content = get($url); foreach $company(@companies) { if ($content =~/$company/smi){ print "$company" } }
it runs, but doesn't print out anything ... even though I know that one of the two companies is on the webpage.

I already tried all match controls (i.e. g,m,s,i in all combinations) to no avail.

thanks for any tips,

cdherold

Replies are listed 'Best First'.
Re: If statement in Foreach loop
by runrig (Abbot) on Aug 18, 2001 at 02:32 UTC
    ...though I know that one of the two companies is on the webpage

    Look at this line:

    @companies = ("FOO,BAR");
    You are setting companies to an array with one element, the string "FOO, BAR". You want to set it to two elements in one of the following ways:
    @companies = ("FOO", "BAR"); # or @companies = qw(FOO BAR);
    better yet, you could set your regular expression to match either one and ditch the for loop:
    # Use quotemeta if companies are not really # foo and bar and contain metacharacters. my $companies = join("|", map(quotemeta, qw(FOO BAR))); print "$1\n" if $content =~ /($companies)/;
Re: If statement in Foreach loop
by rchiav (Deacon) on Aug 18, 2001 at 02:11 UTC
    Couple things. The first thing you should do is print $content to make sure it contains what you think it contains.

    Second, you should quote $company in your regex. Like so..

    if ($content =~/\Q$company\E/is){
    That makes sure that it looks at what's in $company as all text. If you just put $company there, if there are any special characters, it will treat them as those special characters.

    Hope this helps..
    Rich

      A followup on checking whether $content doesn't have what you think it does. A very common and easy way to make that mistake to make is to have a typo in a variable name. This can be very difficult to spot since your logic looks absolutely correct. (Your eye tends to fill in what it expects to see, so you skip over the typo again and again.)

      However using strict.pm will catch the vast majority of these errors. Which is why it is good to be in the habit of writing strict-compliant code and then to use strict. For instance in this case:

      foreach my $company (@companies) { # yada, yada }
Re: If statement in Foreach loop
by perrin (Chancellor) on Aug 18, 2001 at 01:15 UTC
    Have you checked to make sure $content actually has something in it? There could be a problem with your get($url) subroutine. When you're debugging, you shouldn't take anything for granted.