[tpm] YARE (yet another regex) question

Madison Kelly linux at alteeve.com
Fri Aug 1 10:44:31 PDT 2008


Dave Doyle wrote:
> Hi Madision,
> 
> I'm not sure I get what you're doing
> 
> The foreach doesn't return a list for each iteration (which is what 
> you're doing by trying to assign to (my $variable, my $value).  It 
> returns one scalar for each iteration of the loop.  The foreach just 
> iterates over an array.  If you do a regex in a foreach with /g it 
> returns what's captured as a list but you only get key OR value for each 
> iteration.
> 
> Why would you not just do this?
> 
> (Apologies for the formatting)
> 
> my $results_page = qq/
>     <input name="varname" type="hidden" value="varval">
>     <input name="varname2" type="hidden" value="varval2">
> /;
> my %results = ();
> while (
>     $results_page =~
>     /<input name="(.*?)" type="hidden" value="(.*?)">/gs
> ) {
>     my ($var,$val) = ($1, $2);
>     print "Storing: [$var]\t->\t[$val]\n";
>     $results{$var}=$val;
> }
> 
> I don't see how the saved line is of any benefit in this instance.
> 
> Further to this, regexes to parse html isn't ideal.  There's any number 
> of changes in the HTML that could break this (ordering of the 
> attributes, case of the tags/attributes, using single instead of double 
> quotes to delimit the values, etc...)
> 
> D

Hi,

   Thanks for this, and it is what I will do. I had hoped to save a line 
of code, is all, so it's not a big deal. I am not terribly surprised 
that what I wanted to do wouldn't work. Oh well. :)

Madi


More information about the toronto-pm mailing list