Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: adding an IF to a push

by Zaxo (Archbishop)
on Jun 28, 2006 at 03:48 UTC ( [id://557917]=note: print w/replies, xml ) Need Help??


in reply to adding an IF to a push

Your proposed if form is correct. The parens are unnecessary for a "statement modifier" if like that, but they don't do any harm if you prefer to keep them.

You can link two conditions with and if you want to fail unless both are true. The additional processing you need can be wrapped up in a do block.

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

After Compline,
Zaxo

Replies are listed 'Best First'.
Re^2: adding an IF to a push
by sulfericacid (Deacon) on Jun 28, 2006 at 03:56 UTC
    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
      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
        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; };

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (8)
As of 2024-04-24 10:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found