Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^2: adding an IF to a push

by sulfericacid (Deacon)
on Jun 28, 2006 at 03:56 UTC ( #557918=note: print w/ replies, xml ) Need Help??


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

Although Zaxo is right, I imagine it'd be okay, too, if the condtional statements were wrapped in a sub{} and you just check the return value in the proposed if().



"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


Comment on Re^2: adding an IF to a push
Re^3: adding an IF to a push
by ikegami (Pope) on Jun 28, 2006 at 04:40 UTC
    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.

      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);
      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; };
        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}

        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:

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2014-12-27 08:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (176 votes), past polls