Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^3: adding an IF to a push

by ikegami (Patriarch)
on Jun 28, 2006 at 04:40 UTC ( [id://557924]=note: print w/replies, xml ) Need Help??


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

You mean like the following?
push @array, $string =~ m#stuff#g if sub { my $img = get($image); my ($height, $width) = imgsize(\$img); $height < $max_h and $width < $max_w; }->();

Zaxo's do is more direct and has less overhead, but why not just use the following:

my $img = get($image); my ($height, $width) = imgsize(\$img); push @array, $string =~ m#stuff#g if $height < $max_h and $width < $max_w;

You can wrap the whole thing in curlies if you want to limit the scope of $img, $height and $width.

Replies are listed 'Best First'.
Re^4: adding an IF to a push
by sulfericacid (Deacon) on Jun 28, 2006 at 04:45 UTC
    Actually I was going to go out on a lim and try
    my $num = 5; if (\&add($num) { .. } sub add { my $shift = shift; my $num2 = 10; my $sum = $num1 + $num2; if ($sum == "15") { return 1; } else { return 0; } }
    But after playing through numerous attempts, I couldn't pull it off. I just figured you should be able to test the return of a sub this way, though I've never thought of trying it until now.


    "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

    sulfericacid

      Change
      if (\%add($num) { .. }
      to
      if (add($num)) { .. }

      In context, it would actually be:

      sub is_image_size_ok { my ($image) = @_; my $img = get($image); my ($height, $width) = imgsize(\$img); return $height < $max_h and $width < $max_w; } push @array, $string =~ m#stuff#g if is_image_size_ok($image);
        Thank you ikegami for clearing that up. I knew it was possible but I couldn't get the syntax to work although now that I look back, I have to wonder what I was thinking in the first place. Easy stuff.


        "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

        sulfericacid
Re^4: adding an IF to a push
by Anonymous Monk on Jun 28, 2006 at 05:40 UTC
    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; };

      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:

      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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (9)
As of 2024-04-23 07:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found