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

Here's the loop I wrote:
do { my @profiles = $mech->content =~ /\.cf +m?member=(.*)\" class=/g; chomp @profiles; foreach my $parse (@profiles) { if (@profiles){ $numPro++; open (OUT, $proList) || p +rint "cant open profiles file!"; print OUT "$parse\n"; close(OUT); } } $currPage++ ; print "$currPage $numPro\n\n"; } while ( $currPage <= 50 );
However, I'm not receiving any matches! :(

Replies are listed 'Best First'.
Re: Pattern Matching Issues
by graff (Chancellor) on Apr 27, 2007 at 03:23 UTC
    So... have you tried inspecting what is contained in / returned by  $mech->content ? How is that being set?

    Maybe you just need to put a backslash in front of the question mark in that regex:  /\.cfm\?member=(.*)\" class=/g because the question mark is really a regex operator, meaning "0 or 1 occurrences of the preceding character or group".

    By the way, assuming that you are not setting "$/" to some special (non-default) value, the chomp is really a no-op, because your regex will never return a string that ends in a linefeed.

    Update: I have to throw in a couple extra nit-picks: Since you are not changing the name of the output file inside the foreach loop, your process will work better if you open the output file only once, before the loop, and close it only once, after the loop. This won't make a big difference for small jobs, but an open/close on every iteration of a loop like that "does not scale well" when you get into large numbers of rapid iterations. Also, consistent indentation of source code is worthwhile.

Re: Pattern Matching Issues
by jwkrahn (Abbot) on Apr 27, 2007 at 04:31 UTC
    foreach my $parse (@profiles) { if (@profiles){
    Your if statement is superfluous because you only get to the if statement if @profiles is true (contains any elements.) If @profiles is empty (false) then the foreach loop will not even run.
    open (OUT, $proList) || print "cant open +profiles file!"; print OUT "$parse\n";
    You are opening the file $proList for INPUT so you cannot write to it. You need to open it for output or append.
Re: Pattern Matching Issues
by GrandFather (Saint) on Apr 27, 2007 at 03:20 UTC

    What does $mech->content contain?


    DWIM is Perl's answer to Gödel