in reply to foreach loop with nested
In general, the construction
$content =~ /foo(.*?)bar/; $new_coverage = $1;
Is cumbersome and error-prone -- if that match doesn't occur, $1 won't contain what you expect it to contain. So do it in one step -- capture the match into a variable, and see if it's defined (this version will grab each match and display it):
while (my ($new_coverage) = ($content =~ /$company(.*?)<\/tr>/ig ) ) { print "$new_coverage\n"; }
update wrote that a little quickly. runrig pointed out, quite correctly, that this regex will produce an infinite loop. What will work is
if (my @new_coverage = ($content =~ /$company(.*?)<\/tr>/isg) ) { print "$_\n" foreach @new_coverage; }
props, of course, to runrig </update>
I'm guessing that you're snagging a web page with news about these companies, *and* that there may be more than one content item per company (otherwise the /g modifier and the while loop don't make sense). If this is what you're doing, there might be a need to add the /s to that regex as well (because you may want . to match a newline).
perl -e 'print "How sweet does a rose smell? "; chomp ($n = <STDIN>); +$rose = "smells sweet to degree $n"; *other_name = *rose; print "$oth +er_name\n"'
|
|---|