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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

uninitialized value error 1

Status
Not open for further replies.

PerlDaniel

Programmer
Apr 27, 2005
10
US
This is a newbie question, but I am not sure why I am getting this error.

tia

Use of uninitialized value in addition (+) at line 18, <STDIN> line 6.
Use of uninitialized value in concatenation (.) or string line 21, <STDIN> line 6.

print "Enter column: \n";
chomp (my @width =<STDIN>);

print "Enter line: \n";
chomp (my @lines =<STDIN>);

print "1234567890" x (($width+9)/10), "\n";

foreach (@lines) {
printf "%${width}s \n", $_;
}
 
Good to see you're using warnings. Those are telling you that you're trying to use variables whose values are undefined. It warns you because that's almost never what you're trying to do. Can you tell us which lines are line 18 and line 21? - it should help pinpoint the problem better.
 
What happens when you assign STDIN to an array? Does it just keep going when you press enter, and stop when you CTRL-D? If so, having two of them looks like a recipe for disaster.

Put
Code:
use strict;
use warnings;
at the top of your program and see what you get. You should probably put these in all your code as a matter of course - they help trap a lot of silent errors. You can also
Code:
use diagnostics;
to get a more verbose description of the error. While you are learning, it can be quite useful.
 
line 18 is:
print "1234567890" x (($width+9)/10), "\n";

line 21 is:
printf "%${width}s \n", $_;

tia
 
Your $width variable doesn't have a value. Where is it being declared?
 
use diagnostics seems very useful. w/o use diagnostics, <STDIN> takes inputs fine. w/ use diagnostics, I am getting:

Global symbol "$width" requires explicit package name
Global symbol "$width" requires explicit package name
Execution aborted due to compilation errors (#1)
F) You've said "use strict vars", which indicates that all variables
must either be lexically scoped (using "my"), declared beforehand using "our", or explicitly qualified to say which package the global variable is in (using "::").

Uncaught exception from user code:
Global symbol "$width" requires explicit package name
Global symbol "$width" requires explicit package name
Execution of ex6.pl aborted due to compilation errors.

tia
 
I have tried:

my @width =();
print "Enter column: \n";
chomp (@width =<STDIN>);

but, I am getting the same error. Am I doing wrong?

tia
 
Those lines refer to @width, which an array. When you're printing, you're using $width, which is a scalar. They are different variables. You've declared @width ok but you haven't declared $width anywhere.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top