### 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??

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

Replies are listed 'Best First'.
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;

{
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
to

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

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}```

Create A New User
Node Status?
node history
Node Type: note [id://557918]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2018-01-16 13:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
How did you see in the new year?

Results (179 votes). Check out past polls.

Notices?