2

I have the following code:

my $sDatabase = "abc_def:xyz_comp.";
if ($sDatabase =~ m/^(\w)*\:(\w*)\_em\.$/)
{
    print "$1\:$2\.\n";
}
else
{
    print "$1\:$2\_em\.\n";
}

but I am getting empty $1 and $2. The output is:

Use of uninitialized value in concatenation (.) or string at new_mscn_iden_parse.pl line 187.
Use of uninitialized value in concatenation (.) or string at new_mscn_iden_parse.pl line 187.
:_em.

3 Answers 3

4

This code will do what you want

my $sDatabase = "abc_def:xyz_comp.";

$sDatabase =~ m/^(\w+):(\w+?)(_em)?\.$/ or die "Invalid data";
if ($3) {
  print "$1:$2.\n";
}
else {
  print "$1:$2_em.\n";
}
4
  • if $sDatabase = "abc_def:xyz_comp_em." then i want $sDatabase = "abc_def:xyz_comp." Please help.
    – MI.Sarwar
    Apr 29, 2013 at 19:32
  • How about just unconditionally print "$1:$2$3.\n" after surviving the m//?
    – pilcrow
    Apr 29, 2013 at 20:44
  • Is (\w+?) a valid regular expression? I think you wanted (\w+)?.
    – David W.
    Apr 29, 2013 at 21:14
  • @David W., no, what he wrote is correct. (That said, I despise the non-greediness modifier, though. Very fragile. You can't use it twice in the same pattern.)
    – ikegami
    Apr 29, 2013 at 22:34
3

What do you expect $1 and $2 to contain when you fail to match?!

It contains whatever it contains before you attempted the match.

Possible solution:

$sDatabase =~ s/(?<!_em)(?=\.\z)/_em/;
8
  • $1 should have abc_def $2 should have xyz_comp
    – MI.Sarwar
    Apr 29, 2013 at 19:12
  • How is that possible? The pattern didn't match.
    – ikegami
    Apr 29, 2013 at 19:13
  • But still i want a result in else condition then. Sorry if i am not making sense. How is that possible?
    – MI.Sarwar
    Apr 29, 2013 at 19:16
  • You're not listening. It's not possible. The match in if (/.../) { ... } else { ... } will not set $1 when execution goes to the else branch, because you never told it what to place in $1. You can't use that pattern.
    – ikegami
    Apr 29, 2013 at 19:21
  • ok thanks. So how to assign "abc_def:xyz_comp." to variable if $sDatabase has value "abc_def:xyz_comp_em." and assign "abc_def:xyz_comp_em." if $sDatabase is equal to "abc_def:xyz_comp."
    – MI.Sarwar
    Apr 29, 2013 at 19:25
1

You have:

my $sDatabase = "abc_def:xyz_comp.";
if ($sDatabase =~ m/^(\w)*\:(\w*)\_em\.$/);

Let's see if this matches:

You're regular expression says:

  • Anchor at the start of a line.
  • You are looking for zero or more word characters . Word characters (in the ASCII alphabet) includes lowercase letters, uppercase letters numbers and underscores.
    • Thus /\w*/ will match all the following:
    • Computer
    • computer
    • computer23
    • computer_32
    • an empty string
  • You're next looking for a colon
  • Then, more word characters
  • Followed by a _em string
  • Followed by a period
  • And that should be the end of the string (if there's no NL and you're not doing multi-line string searches. Looks like you're safe there).

Now, let's look at your string: abc_def:xyz_comp.

  • \w* will match up to abc_def. Regular expressions are greedy and will try to match the biggest portion of the string as possible.
  • The : will match the colon. So far, you're matching abc_def:.
  • That \w* will match on xyz_comp.
  • Now, you're trying to match a _em. Oops! No good. There is no _em in your string. Your regular expression match will fail.

Since your regular expression match fails, the $1 and $2 variables simply are not set and have no value.

That's why you're getting Use of uninitialized value. What you can do is make the later half of your expression optional:

my $sDatabase = "abc_def:xyz_comp.";
if ($sDatabase =~ /^(\w)+:(\w*)(_em)?\.$/) {
    if ( $3 ) {
        print "$1:${2}${3}.\n";
    else {
        print "$1:${2}_em.";
    }
}
else {
   die qq(String doesn't match regular expression at all\n);
}

}

First of all, I think you want to match at least one character (I could be wrong), so I switched the asterisk which matches zero or more to a + which matches one or more.

Note I have a third set of parentheses followed by a ?. This means match this zero or one times. Thus, you will have a match, and $1 and $2 will be set as long as your string starts with one or more word characters, followed by a colon, followed by one or more word characters.

What won't necessarily happen is that $3 will be set. This will only be set if your string also ends with _em.. If your string doesn't include the _em, but ends with a period, $1 and $2 will still match.

In your case, we could simplify it by doing this:

my $sDatabase = "abc_def:xyz_comp.";
if ($sDatabase =~ /^(\w)+:(\w*)(?:_em)?\.$/) {
    print "$1:${2}_em.";
else {
   die qq(String doesn't match regular expression at all\n);
}

The (?:...) means don't set a match, just group. Thus, $3 will never be set. That's okay, either $3 is _em. or we add _em. to the end of the match anyway.

1
  • (?:_em)? will always match the empty string. You'll end up doubling _em if it's present.
    – ikegami
    Apr 29, 2013 at 22:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.