f



assigning alternativ value if a regular expression has failed

Hello,

I am using regular expressions to filter words and numbers from $_ and 
assign them to $1 and $2 for further use

$_ =~ m/(.*?)(-{0,1}\d+)$/;

How can I assign an alternative value to $1 and $2 if the regular 
expression failed? (sometimes no there is no input, so I want to record 
a failure message)

Problem is: the digit "0" is a possible value in $1 or $2

I was trying

if(!$1){
   $var="alternativ values";
}
else{
   $var=$1;
}

or

if($1 eq undef){
   $var="alternativ values";
}
....

How cann I check whether a $scalar exists or not? Is there something 
like the "hash exists-function"? Thanks for helping me.

Rocco

0
Rocco
7/14/2003 4:53:58 PM
comp.lang.perl.misc 33233 articles. 2 followers. brian (1246) is leader. Post Follow

3 Replies
559 Views

Similar Articles

[PageSpeed] 38

X-Ftn-To: Rocco Melzian 

Rocco Melzian <Rocco.Melzian@TU-Berlin.de> wrote:
>if($1 eq undef){
>   $var="alternativ values";
>}
>...
>
>How cann I check whether a $scalar exists or not? Is there something 
>like the "hash exists-function"? Thanks for helping me.

You can put the regex in 'if' condition,

if (m/(.*?)(-{0,1}\d+)$/) {
....
}

hope this helps,


-- 
Matija
0
Matija
7/14/2003 5:36:03 PM
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Rocco Melzian <Rocco.Melzian@TU-Berlin.de> wrote in news:beumvk$jcp$1
@mamenchi.zrz.TU-Berlin.DE:

> Hello,
> 
> I am using regular expressions to filter words and numbers from $_ and 
> assign them to $1 and $2 for further use
> 
> $_ =~ m/(.*?)(-{0,1}\d+)$/;
> 
> How can I assign an alternative value to $1 and $2 if the regular 
> expression failed? (sometimes no there is no input, so I want to record 
> a failure message)

unless ( ($thing1, $thing2) = /(.*?)(-{0,1}\d+)$/ )
{
    $thing1 = 'default thing one';
    $thing2 = 'default thing two';
    print LOG "Had to use default values\n";
}

- -- 
Eric
$_ =  reverse sort qw p ekca lre Js reh ts
p, $/.r, map $_.$", qw e p h tona e; print

-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

iQA/AwUBPxMgdGPeouIeTNHoEQLGUQCg6zymsS2tXujPesK43sG/2EU1NUQAniuE
FSpfwtQI08YAznTZ1S4aRgLs
=Rx/s
-----END PGP SIGNATURE-----
0
Eric
7/14/2003 9:27:56 PM

LaDainian Tomlinson wrote:
> "Rocco Melzian" wrote:
> 
>>I am using regular expressions to filter words and numbers from $_ and
>>assign them to $1 and $2 for further use
>>
>>$_ =~ m/(.*?)(-{0,1}\d+)$/;
> 
> 
> To make things a little simpler, the m// operator operates on $_ by default,
> the 'm' is not required if you are using forward slashes as delimiters, and
> the {0,1} construct is exactly the same as '?' in that context.  So your
> line is equivalent to:
> 
> /(.*?)(-?\d+)$/;
> 
> Check up on perlop under "Regexp Quote-Like Operators" for info on m// and
> perlre for info on regular expressions.
> 
> 
>>How can I assign an alternative value to $1 and $2 if the regular
>>expression failed? (sometimes no there is no input, so I want to record
>>a failure message)
> 
> 
> In scalar context, m// also returns true if the match succeeds and false if
> it doesn't.
> 
> if (/(.*?)(-?\d+)$/){
>     $word = $1;
>     $number = $2;
> } else {
>     print "Match failed.";
> }
> 
> 
>>How cann I check whether a $scalar exists or not? Is there something
>>like the "hash exists-function"? Thanks for helping me.
> 
> 
> You can try defined().  But most of the regexp operators will give you an
> easy way to check if (or how many times) the match succeeded, so you
> shouldn't really need to check whether the $n variables are defined, since
> that may also give you unexpected results based on an earlier match.
> 
> Hope that helps,
> 
Thank you very much. I will work out how I can check the regexp return 
values. Meanwhile I use the if-method. Cheers.

Rocco

0
Rocco
7/15/2003 7:45:42 AM
Reply: