### 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?
 [1nickt]: Corion HPs? ugh. I was impressed with Lenovo's gaming laptops; if it weren't for the red backlighting and general flashiness of the aesthetic, I might have gone with that. But all that was until I discovered that the Dell Precision line is still around. [1nickt]: stevieb I don;t doubt that there's a difference. BestBuy has "consumer" models only on display. [ambrus]: 1nickt: for some reason, these days they call every computer "gaming", even ones that gamers wouldn't buy. I've bought a keyboard that was labelled "gamer", despite that it has hard springs and seems to be way better for typing than for gaming; [1nickt]: I though the gamers like that because they bash the keys so hard.

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (14)
As of 2017-03-27 13:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Should Pluto Get Its Planethood Back?

Results (320 votes). Check out past polls.