Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi guys. I have a push connected to a regex. I need to now add a conditional to it and was wondering if you could show me the easiest way?
push @array, $string =~ m#stuff#g;
Would this be correct?
push @array, $string =~ m#stuff#g if ($this eq "true");
If so, I need to add something onto that one and wouldn't know how to do that. I'd need to push it only IF I do an image::size on it
my $img = get($image); my ($height, $width) = imgsize(\$img);
I need to check to make sure height and width are less than $max_h and $max_w and ONLY if both are, then push it.

Replies are listed 'Best First'.
Re: adding an IF to a push
by Zaxo (Archbishop) on Jun 28, 2006 at 03:48 UTC

    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

      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.

Re: adding an IF to a push (grep)
by tye (Sage) on Jun 28, 2006 at 05:20 UTC
    push @a, grep { my $img= get($_); my( $h, $w )= imgsize( \$img ); $h <= $maxh && $w <= $maxw; } $str =~ /.../g;

    - tye        

Re: adding an IF to a push
by GrandFather (Saint) on Jun 28, 2006 at 04:03 UTC

    If I understand what you want to achieve then something like this would seem to be what you are after:

    if ($this eq "true") { my $img = get($image); my ($height, $width) = imgsize(\$img); push @array, $string =~ m#stuff#g if $height < $max_h && $width < +$max_w; }

    DWIM is Perl's answer to Gödel
      This post like the one from the other day asking for a regex to push images onto. If that's the case, that regex would have to match prior to testing it's size.

      Though I could be wrong entirely. I'll link the node if ever I find it

      UPDATE: looked and looked and cannot find it. Maybe it was Tek-Tips or another site I'm thinking of.



      "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