[tpm] YARE (yet another regex) question

Dave Doyle dave.s.doyle at gmail.com
Fri Aug 1 10:07:22 PDT 2008

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";

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...)


On Fri, Aug 1, 2008 at 12:17 PM, Madison Kelly <linux at alteeve.com> wrote:

> Fulko Hew wrote:
>> On Fri, Aug 1, 2008 at 10:50 AM, Madison Kelly <linux at alteeve.com<mailto:
>> linux at alteeve.com>> wrote:
>>     As an expansion to my earlier question on assigning values to new
>>    variables directly from a regular expression; I want to now use a
>>    similar technique to populate a 'foreach' loop.
>> ... snip ...
>>    However, this doesn't:
>>    my $results_page=$agent->content;
>>    my %results=();
>>    foreach (my $variable, my $value) ($results_page=~/<input
>>    name="(.*?)" type="hidden" value="(.*?)">/gs)
>>                                  ^^
>> How about... you forgot the equal sign for the assignment!
> Doesn't look like this is the case (tried it to be sure), or if it is, I am
> note sure where the '=' would go for the assignment. As I understand the
> 'for'/'foreach' syntax is that is automatically pops a value off the array
> per iteration.
> Not that I am not often wrong. :)
> Madi
> _______________________________________________
> toronto-pm mailing list
> toronto-pm at pm.org
> http://mail.pm.org/mailman/listinfo/toronto-pm

dave.s.doyle at gmail.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/toronto-pm/attachments/20080801/9526cb7d/attachment-0001.html>

More information about the toronto-pm mailing list