Win has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I have this error message:
Use of uninitialized value in numeric eq (==) at Creating_table_D.pl l +ine 31. Use of uninitialized value in numeric eq (==) at Creating_table_D.pl l +ine 35.
for the following offending code:
sub create_table ($) { my($schema_in) = $_; if ($schema_in == "1"){ # offending line 1 my @tables = qw/A/; } if ($schema_in == "2"){# offending line 2 my @tables = qw/A B C/; }

I have tried a number of variations on a theme. Does anyone have a quick explanation?

Replies are listed 'Best First'.
Re: uninitialized value in numeric eq (==)
by edan (Curate) on Nov 11, 2003 at 11:46 UTC

    Try changing:

    sub create_table ($) { my($schema_in) = $_;

    to:

    sub create_table { my ($schema_in) = @_;

    See if that helps.

    Read perlvar for more info on the difference between $_ and @_ (they are very different, even though they look similar)

    --
    3dan

Re: uninitialized value in numeric eq (==)
by Roger (Parson) on Nov 11, 2003 at 11:53 UTC
    You can either change your code -
    my($schema_in) = $_;
    to this -
    my($schema_in) = @_;
    or this -
    my $schema_in = shift;
Re: uninitialized value in numeric eq (==)
by rdfield (Priest) on Nov 11, 2003 at 11:45 UTC
    • Why put quotes around the numbers 1 and 2?
    • Why use a prototype?
    • have you checked the value of $schema_in?

    rdfield

      rdfield is right on the money.

      Pay attention to exactly what error message are trying to tell you - they are usually pretty easy to understand if you take a minute and really look at it. Notice

      Use of uninitialized value in numeric eq (==) at Creating_table_D.pl line 31. Use of uninitialized value in numeric eq (==) at Creating_table_D.pl line 35.
      Here's your code:
      sub create_table ($) { my($schema_in) = $_; if ($schema_in == "1"){ # offending line 1 my @tables = qw/A/; } if ($schema_in == "2"){# offending line 2 my @tables = qw/A B C/; }

      Notice in particular Use of uninitialized value. This probably means that $schema_in has *NO* value. As rdfield suggested, do some debugging - print out the value of $schema_in in subroutine 'create_table' to see if it is receiving the value you think it's receiving.

      Notice another thing - when doing a numeric comparison, you don't want to test using *string* "1" - you want to test using number 1 - without quotes around it. If you include the quotes and $schema_in contains a number, then perl will have to convert the string to a number and then do the numeric comparison.

      HTH.

Re: uninitialized value in numeric eq (==)
by EvdB (Deacon) on Nov 11, 2003 at 11:50 UTC
    Change my($schema_in) = $_; into my $schema_in = shift; and it works.

    $_ is not set on enty into sub routine, @_ is.

    --tidiness is the memory loss of environmental mnemonics