Help - Search - Member List - Calendar
Full Version: uninitialized value?
WorkTheWeb Forums > Webmaster Resources > Perl Beginner Help
Support our Sponsors!
Chad Smith
Hello,

My script works but I keep getting one of the following 2 frustrating errors:

1- Use of uninitialized value in string eq...
2- Use of uninitialized value in concatenation (.) or string...

The error occurs the fourth time the subroutine is called. According to perl.com, the error means - "This means that you're trying to use a variable before you've assigned a value to it" I think the problem may be in the code snippet below, could it that $_[0] needs to be reinitialized? If so, how?

my array = ("", "Y", "N");

function($scalar_value1);

sub function {
my ($value, $item, $selected);
$value = $_[0]; #$_[0] should eq first (and only) value of $scalar_value1
foreach $item(@array) {
if ($item eq $value){
$selected = "selected";
}
print "<option value="$item" $selected>$item</option>n";
}
}



--
_______________________________________________
NEW! Lycos Dating Search. The only place to search multiple dating sites at once.
http://datingsearch.lycos.com

Chris Devers
On Tue, 5 Jul 2005, chad smith wrote:

QUOTE
My script works but I keep getting one of the following 2 frustrating errors:

1- Use of uninitialized value in string eq...
2- Use of uninitialized value in concatenation (.) or string...

The error occurs the fourth time the subroutine is called.  According
to perl.com, the error means - "This means that you're trying to use a
variable before you've assigned a value to it"  I think the problem
may be in the code snippet below, could it that $_[0] needs to be
reinitialized?  If so, how?

my array = ("", "Y", "N");
^^^^^^


Spot the bug @ this line :-)

QUOTE
function($scalar_value1);

sub function {
my ($value, $item, $selected);
$value = $_[0];
#$_[0] should eq first (and only) value of $scalar_value1
foreach $item(@array) {
if ($item eq $value){
$selected = "selected";
}
print "<option value="$item" $selected>$item</option>n";
}
}

Rather than specifying what you want to do with function()'s argument,
you're just hoping that things magically work. Don't do that. be
explicit about it, right up front, before doing anything else.

function( $scalar_value );

sub function {
my $arg = shift; # or examine $_, @_, etc
my ( $value, $item, $selected );
...

That in turn means rewriting the $_[0] using code, but that's a good
idea anyway. Inconsistency there may be what's causing the problem in
the first place.


Also, you never seem to use array [nee @array]; if there's supposed to
be some kind of relation between @array and $scalar_value1, then you
need to spell it out.



--
Chris Devers


PHP Help | Linux Help | Web Hosting | Reseller Hosting | SSL Hosting
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2005 Invision Power Services, Inc.