On Tue, 5 Jul 2005, chad smith wrote:

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 :-)

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

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to