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

Tryin to noodle this out and looking for a some help. Basically, I want to set a value to the match for a regex, UNLESS it didnt' match, and then I want to set a default value for this var.. e.g.</p.

...snip... if( $string =~ /foobar/ ) { $match = $1 unless undef then $match = "chiapet"; ]

Now, obviously this silly code doesn't work, but how would I go about acheieving this? Thanks a lot in advance.

Replies are listed 'Best First'.
Re: Regex question
by thelenm (Vicar) on Mar 19, 2004 at 22:07 UTC

    What I would do is set the default value first, then try the match. Something like this:

    $match = "chiapet"; # default value if ($string =~ /foobar(...)/) { # probably want to capture something +here $match = $1; }

    Or the ternary ?: operator, like this:

    $match = ($string =~ /foobar(...)/) ? $1 : "chiapet";

    -- Mike

    --
    XML::Simpler does not require XML::Parser or a SAX parser. It does require File::Slurp.
    -- grantm, perldoc XML::Simpler

Re: Regex question
by Happy-the-monk (Canon) on Mar 19, 2004 at 22:09 UTC

    if ( $string =~ m/(foobar)/ ) { $match = $1; } else { $match = "chiapet"; }

      Uch.. problem is I'm doing a BUNCH of stuff within the "if" loop based on this match..

      if ( $string =~ m/(foobar)/ ) { $match = $1; push (@array, $match); bunch of other stuff... }

      I'll try and do some more fiddling with the else though..

      Thanks

        problem is I'm doing a BUNCH of stuff within the "if" ...

        I can't see where there would be a problem yet. Do you care to explain?

        On a side note: if does no loop. Call it an "if-block". Cheers, Sören

Re: Regex question
by NetWallah (Canon) on Mar 19, 2004 at 22:08 UTC
    Set the default value BEFORE the "if".

    You will get inside the IF only if it is already matched.

    Offense, like beauty, is in the eye of the beholder, and a fantasy.
    By guaranteeing freedom of expression, the First Amendment also guarntees offense.

      but.. I'm doing a bunch of stuff IN the if based on this match... =).. so setting the var outside of the if didn't help me.

      Thanks though

        To reiterate NetWallah's point, you won't get into the loop if the regex doesn't match, meaning that if you do get into the loop, there will be something in $1, so any code to set a default value won't be called. If you have to run what is presently in the if block regardless of rather or not the match was successful, then it probably shouldn't be an if block.

        You may wish to use something along the lines of thelenm's second snippet instead, something like this:

        $match = ($string =~ /foobar(...)/) ? $1 : "chiapet"; push (@array, $match); bunch of other stuff...

        note the lack of the if block...

        Just Another Perl Alchemist
Re: Regex question
by Somni (Friar) on Mar 22, 2004 at 13:04 UTC

    You may have missed it, but based on your description and the subsequent discussion thelenm already gave you your answer.

    $match = $string =~ /foobar/ ? $1 : "chiapet"; ... the rest of your code goes here ...

    Although you may want to consider what you're doing; "chiapet" is harder to check if you have code that does something slightly different if the value was defaulted. On the other hand, undef is much easier to check.

    Depending on your regex you may also lose information by defaulting it, which may be undesireable; where undef is easy to distinguish (a string can't contain undef), defaulting the value will be indistinguishable from "chiapet" in the string itself.