Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use of uninitialized value in pattern match (m//) 1

Status
Not open for further replies.

CristianLuca

Programmer
Oct 25, 2007
36
RO
Hello guys,

* I get this error when parsing the following lines :

start clusterNo=1
value chunkNo=3
....................

* the code :
while ($line <= $#$_lines) {
chomp $$_lines[$line];
if ($$_lines[$line] =~ /start\s(\S*)=(\d*)/) {
$clusterNo = $2;
}
.....

When i'm parsing the second line (value chunkNo=3) the error occurs. Eny ideea why ?

Thanks in advance,
Cristian
 
First of all, it's not an error, it's a warning. And it's imply telling you the value that variable that you are matching in your regex is undefined. You can test for definition first to avoid the error, but the ultimate question should be, is this a signal of a greater problem? Only you can answer that.

Code:
[olive][b]if[/b][/olive] [red]([/red][url=http://perldoc.perl.org/functions/defined.html][black][b]defined[/b][/black][/url] [blue]$$_lines[/blue][red][[/red][blue]$line[/blue][red]][/red] && [blue]$$_lines[/blue][red][[/red][blue]$line[/blue][red]][/red] =~ [red]/[/red][purple]start[purple][b]\s[/b][/purple]([purple][b]\S[/b][/purple]*)=([purple][b]\d[/b][/purple]*)[/purple][red]/[/red][red])[/red] [red]{[/red]

There are two additional stylistic comments I would make as well. $line appears to be a simple index iterator for the array @$_lines. If this is so, I would strongly advice you use a for loop instead of a while.

Code:
[olive][b]for[/b][/olive] [url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$line[/blue] [red]([/red][fuchsia]0..[/fuchsia][blue]$#$_lines[/blue][red])[/red] [red]{[/red]

The other thing I would point out is your use of an underscore prefixed variable. I'm not sure what style convention that's supposed to be, but it simply strikes me as odd when out of context with the rest of the code.

That's it.

- Miller
 
$$_lines[$line] is defined , i print the value before using it and its not null and even if i add defined $$_lines[$line] && i still get the warning.

And i use while instead of for because i modify the index while in the loop.

Cristian

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top