[tpm] YARE (yet another regex) question

Henry Baragar Henry.Baragar at instantiated.ca
Fri Aug 1 14:04:41 PDT 2008


On Friday, August 01 2008 01:44 pm, Madison Kelly wrote:
> 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.
> >
Hi,

Perhaps the following saves the right number of lines and does a good job of  
showing the intent:

	my %results = 
		$results_page =~  /<input name="(.*?)" type="hidden" value="(.*?)">/g;
	print "Storing: [$_]\t->\t[$result[$_}\n" for keys %result;

Note that this code has not been tested and probably prints the "Storing" 
messages in a different order.

Cheers,
Henry

> > 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
> _______________________________________________
> toronto-pm mailing list
> toronto-pm at pm.org
> http://mail.pm.org/mailman/listinfo/toronto-pm


More information about the toronto-pm mailing list