assigning alternativ value if a regular expression has failed

R

Rocco Melzian

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
 
M

Matija Papec

X-Ftn-To: Rocco Melzian

Rocco Melzian said:
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,
 
E

Eric J. Roode

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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-----
 
R

Rocco Melzian

LaDainian said:
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.




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.";
}




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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top