Rahul Desai
Rahul Desai

Reputation: 15501

append elements into an array element

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.

Upvotes: 1

Views: 1285

Answers (2)

beresfordt
beresfordt

Reputation: 5222

$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.

Upvotes: 0

AnonGeek
AnonGeek

Reputation: 7948

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.

Upvotes: 3

Related Questions