Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^4: adding an IF to a push

by Anonymous Monk
on Jun 28, 2006 at 05:40 UTC ( [id://557933]=note: print w/replies, xml ) Need Help??


in reply to Re^3: adding an IF to a push
in thread adding an IF to a push

Hi. I have another question. The variable I test with is the data caught in the match ($1). I can't get $1 to hold a value. Is there another way to do this?
push @found_images, $get_gal =~ m#http://images\.imagefap\.com/imag +es/thumb/\d+/\d+/\d+\.jpg#g if do { my $image = get($1); print "found $1<br>"; my ($height, $width) = imgsize(\$image); $height < $max_height and $width < $max_width; };

Replies are listed 'Best First'.
Re^5: adding an IF to a push
by ikegami (Patriarch) on Jun 28, 2006 at 06:01 UTC

    Two reasons:

    • You don't have any captures in your regexp, so $1 is never given a value.

    • The do is executed before the match. The match might not be executed at all, depending on the value returned by do.

    Fix:

    while ($get_gal =~ m#(http://{...}.jpg)#g) { my $image = get($1); my ($height, $width) = imgsize(\$image); push @found_images, $1 if $height <= $max_height and $width <= $max_width; }

    Bunch of alternative styles:

Re^5: adding an IF to a push
by shmem (Chancellor) on Jun 28, 2006 at 06:01 UTC
    You have to insert parens into your m## operation
    m#(http://images\.imagefap\.com/images/thumb/\d+/\d+/\d+\.jpg)#g
    or use $&
    # update: this won't work anyways - see previous post and followup push @found_images, $get_gal =~ m#http://images\.imagefap\.com/imag +es/thumb/\d+/\d+/\d+\.jpg#g if do { my $image = get($&); print "found $1<br>"; my ($height, $width) = imgsize(\$image); $height < $max_height and $width < $max_width; };
    at the expense of speed - see perlvar, section BUGS for that.
    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

      That's not enough to make it work. Check the reply I posted at the same time as you.

      And don't use $&! Simply using it will slow down regexp without captures everywhere in your program.

        if I only typed slower and checked the thread once in a while, I wouldn't have posted seeing your answer... ;-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://557933]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2024-03-28 13:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found