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

Hello Dear Monks

I have a problem concerning paranthesis

I would like to detect the following expression, such as

(23
or
(135

So I have used :

# use strict; ... if (($Delay_id =~ /\(\d\d/) || ($Delay_id =~ /\(\d\d\d/)){ ... }

But I have messages of error, such as :

Use of uninitialized value in concatenation (.) or string at

Replies are listed 'Best First'.
Re: parenthesis regex
by dreadpiratepeter (Priest) on Jun 25, 2008 at 16:51 UTC
    I believe you will find that the warning is because $Delay_id is undefined.
    Or it is an warning elswhere in your code and unrelated, there is nothing structurally wrong with your regex.
    Although I would have used /\(\d{2,3}/ or /\(\d\d\d?/ instead of 2 separate expressions


    -pete
    "Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."

      Thanks a lot for youe help, guys !

      I have just found the source of the problem

      It was because I had values such as (8 so there were not selected by the regex hence the problem

      That's really nice of you all, thanks a lot for your help !

Re: parenthesis regex
by moritz (Cardinal) on Jun 25, 2008 at 16:54 UTC
    Maybe you forgot to assign a value to $Delay_id?

    BTW commenting the use strict; line is about the silliest thing you can do, it installs delayed bugs in your script (badly paraphrasing TimToady).

    This doesn't warn:

    #!/usr/bin/perl use strict; use warnings; my $Delay_id = '(23'; if (($Delay_id =~ /\(\d\d/) || ($Delay_id =~ /\(\d\d\d/)){ print "Matched\n"; }
Re: parenthesis regex
by ikegami (Patriarch) on Jun 25, 2008 at 17:31 UTC
    I agree with apl. You haven't showed us the code that generates that warning. That warning occurs when concatenating an uninitialized variable
    $x . "\n"
    or when interpolating of an uninitialized variable (that's the "or string" of the error message)
    "$x\n"

    Those two snippets produce identical code, thus the common warning.

    You can also get the warning when interpolating into a regexp, but you get a second warning too.

    >perl -we"my $x; /$x/" Use of uninitialized value in regexp compilation at -e line 1. Use of uninitialized value in pattern match (m//) at -e line 1.

    On a side note, the code you showed us is problematic for other reasons

    if (($Delay_id =~ /\(\d\d/) || ($Delay_id =~ /\(\d\d\d/)){

    is the same thing as

    if ($Delay_id =~ /\(\d\d/){

    since anything the second // could match will also be matched by the first //. If you're planning on adding captures, you want

    if ($Delay_id =~ /\((\d\d\d?)/){
Re: parenthesis regex
by apl (Monsignor) on Jun 25, 2008 at 16:52 UTC
    You're not showing us the code that contains the concatenation that produces the error.

    No code from you, no answer from us.

      You're not showing us the code that contains the concatenation that produces the error.

      First I disagreed, because it says "or string" in the warnings, but then I tested:

      perl -wle 'my $a; $a =~ m/foo/' Use of uninitialized value in pattern match (m//) at -e line 1.

      So matching against an undefined value is a different warning, and you are right, the relevant code wasn't quoted in the original question, presumably.