in reply to Re^4: adding an IF to a push
in thread adding an IF to a push
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:
while ($get_gal =~ m#(http://{...}.jpg)#g) { my $image = get($1); my ($height, $width) = imgsize(\$image); next if $height > $max_height or $width > $max_width; push @found_images, $1; }
or
my @found_images = grep { my $image = get($_); my ($height, $width) = imgsize(\$image); $height <= $max_height and $width <= $max_width } $get_gal =~ m#(http://{...}.jpg)#g;
or
sub is_image_size_ok { local $_ = @_ ? $_[0] : $_; my $image = get($_); my ($height, $width) = imgsize(\$image); return $height <= $max_height and $width <= $max_width; } my @found_images = grep is_image_size_ok, map m#(http://{...}.jpg)#g, $get_gal;
|
|---|