1

In my Perl program, I am reading an email by decoding it, splitting it into an array. In the snippet below, I am reading the elements and appending it until I find 'Email' or 'Phone' or 'GoToAssist'.

75  while(!($plain[$j] =~ /[Email|Phone|GoToAssist]/)){     
76      $plain[$x] .= " " . $plain[$j];
77      $j++;
78  }

However, I am getting the following error:

Use of uninitialized value in concatenation (.) or string at test.pl line 76, <GEN0> line 921.
Use of uninitialized value in pattern match (m//) at test.pl line 77, <GEN0> line 921.

The code was working properly previously, I have hardly changed anything. I am wondering what went wrong.

5
  • Have you checked if your data has changed? Commented Jul 10, 2012 at 14:11
  • Yes, I checked. Two elements were changed. But I had removed them using regex. It doesnt work with the previous data either. Commented Jul 10, 2012 at 14:12
  • 1
    If it was working previously and data JUST changed - it has to be that the regex is not working as expected in removing faulty parts of the changed data. I mean, I have no empirical evidence...but WHAT ELSE could it be, haha. Could we get and example entry? Commented Jul 10, 2012 at 14:15
  • 2
    $plain[$j] is undefined. We cannot help you to fix this unless you show your complete code. Also the regex /[Email|Phone|GoToAssist]/ doesn't do what you intend. It uses a character class that matches any of the characters A E G P T a e h i l m n o s t |. You want just /Email|Phone|GoToAssist/. Commented Jul 10, 2012 at 14:22
  • Here is the code gist.github.com/3083791. It is working now for some reason! I wonder whats going on! Commented Jul 10, 2012 at 14:56

2 Answers 2

3

Its not an error, its a warning.

You are getting this warning because $plain[$j] is undefined.

You can use following code to check if plain[$j] is defined or not before appending:

while(!($plain[$j] =~ /[Email|Phone|GoToAssist]/)){     
     if(defined $plain[$j]){
         $plain[$x] .= "&nbsp;" . $plain[$j];
         $j++;
     }
 }

If this doesn't help, do share some more code.

And as @Borodin pointed out, your regex is not doing what you probably want. But this is not related to your question.

Sign up to request clarification or add additional context in comments.

3 Comments

Well, that actually worked! But when I commented these changes, it did work again! I dont know whats going on to be honest. You can find my code here: gist.github.com/3083791 You can see the commented statements line # 77 onwards. Thanks.
@rad are you sure you have used the same data which was giving error earlier?
@rad strange...anyways you can keep if-defined check to prevent future warnings....the string being appended cannot be undefined
0

$plain[$j] is undef. Add some print statements to find out if $j is what you expect it to be. If it's wrong, then find out why it ends up with the wrong value. It it's right, find out why $plain[$j] ends up with the wrong value.

4 Comments

I tried that. It gave me the same errors followed by a million &nbsp;'s. Sorry.
print "plain[x] = '" . ( $plain[$x] || '' ) . "'\n" ; print "plain[y] = '" . ( $plain[$y] || '' ) . "'\n" ;
I am getting plain[j] = '' and plain[x] has about a thousand &nbsp;'s
then $plain[$j] and $plain[$x] are not defined - look into the code which populates them

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.