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

This code works, however I get errors when I run it and they reference line 36. I get these Use of uninitialized value in numeric ne (!=) at line 36 of the script. When I take of use strict and use warnings - but we don't want to do that. I have to do something, ligically with line 36. Use strict does not like this line: "if ($spec_hash{$exch}{$symbol} != 1) {"

Use of uninitialized value in numeric ne (!=) at ./pure_penny_golf_wex +tend.forBrodinpl line 36. etds_extend surplus specialist name: delete from etds_extend where e_r +isk_symbol = 'WINN' and e_exch_dest = 'XISX' Use of uninitialized value in numeric ne (!=) at ./pure_penny_golf_wex +tend.forBrodinpl line 36. etds_extend surplus specialist name: delete from etds_extend where e_r +isk_symbol = 'WTR_S5F4' and e_exch_dest = 'XISX'

WINN and WTR_S5F4 are in the %extend_hash

 This is the %extend_hash

$VAR1 = {'6' => {'IACI' => { 'ARCX' => { 'specialist' => '1', 'penny' +=> '0 }}, 'MCHP' => { 'ARCX' => { 'specialist' => '1', 'penny' +=> '0'}}, 'BC' => { 'AMXO' => { 'specialist' => '1', 'penny' +=> '0'}}, 'WINN' => { 'XISX' => { 'specialist' => '1', 'penny' +=> '0'}}, 'WTR_S5F4' => { 'XISX' => { 'specialist' => '1', 'penny' +=> '0'}}, 'XLY' => { 'CS' => { 'specialist' => '0', 'penny' +=> '1'}} } };

this is the spec_hash - WINN and WTR_S5F4 are not in the %spec_hash

$VAR1 = { 'XISX' => { 'FCEL'=> 1, 'GPS' => 1, 'MCO' => 1, 'DPZ' => 1, 'ENTG' => 1, 'PDS' => 1 } };

this is the script.

1 #!/sbcimp/perl/bin/perl 2 #/sbcimp/etds_extend.pl 3 use DBI ; 4 use strict ; 5 use warnings; 6 7 use Data::Dumper; 8 my %extend_hash = (); 9 my $dbUser = 'nice'; 10 my $dbPass = 'try'; 11 my $dbSid = 'THE_world'; 12 my $dbh = DBI->connect("dbi:Oracle:$dbSid","$dbUser","$dbPass") o +r die( "Couldn't connect: $!" ); 13 my $query = "select level_id,e_risk_symbol,e_exch_dest,penny,speci +alist from etds_extend"; 14 if(!$dbh) { 15 print "Error connecting to DataBase; $DBI::errstr\n"; 16 } 17 my $cur_msg = $dbh->prepare($query) or die "\n\nCould not prepare +statement: ".$dbh->errstr; 18 $cur_msg->execute(); 19 while (my @row=$cur_msg->fetchrow_array) { 20 $extend_hash{$row[0]}{$row[1]}{$row[2]}{'penny'}=$row[3]; 21 $extend_hash{$row[0]}{$row[1]}{$row[2]}{'specialist'}= +$row[4]; 22 } 23 my %spec_hash=(); 24 $query = "select e_risk_symbol from gsd_etds where level_name='EXC +H_CS' and e_exch_dest='XISX' and e_symbol_comment in ('Bin_6','Bin_56 +')"; 25 if(!$dbh) { print "Error connecting to DataBase; $DBI::errstr\n"; +} 26 $cur_msg = $dbh->prepare($query) or die "\n\nCould not prepare + statement: ".$dbh->errstr; 27 $cur_msg->execute(); 28 while (my @row=$cur_msg->fetchrow_array) { 29 $spec_hash{'XISX'}{$row[0]}=1; 30 } 31 #print Dumper(\%spec_hash) ; 32 #print Dumper(\%extend_hash) ; 33 foreach my $symbol (sort keys %{$extend_hash{6}}) { 34 foreach my $exch (sort keys %{$extend_hash{6}{$symbol}}) { 35 if ($extend_hash{6}{$symbol}{$exch}{'specialist'}) { 36 if ($spec_hash{$exch}{$symbol} != 1) { 37 my $line="etds_extend surplus specialist name: del +ete from etds_extend where e_risk_symbol = '$symbol' and e_exch_dest += '$ exch'"; 38 print "$line\n"; 39 #push(@error_array,$line); 40 } 41 } 42 } 43 }

Replies are listed 'Best First'.
Re: Use of uninitialized value
by GrandFather (Saint) on Sep 10, 2014 at 03:31 UTC

    and the question is what?

    Maybe what you need is a test to see if the hash entries exist? The following may help:

    #!/sbcimp/perl/bin/perl use strict; use warnings; my %extend_hash = ( '6' => { 'IACI' => {'ARCX' => {'specialist' => '1', 'penny' => '0'} +}, 'MCHP' => {'ARCX' => {'specialist' => '1', 'penny' => '0'} +}, 'BC' => {'AMXO' => {'specialist' => '1', 'penny' => '0'} +}, 'WINN' => {'XISX' => {'specialist' => '1', 'penny' => '0'} +}, 'WTR_S5F4' => {'XISX' => {'specialist' => '1', 'penny' => '0'} +}, 'XLY' => {'CS' => {'specialist' => '0', 'penny' => '1'} +} } ); my %spec_hash = ( 'XISX' => { 'FCEL' => 1, 'GPS' => 1, 'MCO' => 1, 'DPZ' => 1, 'ENTG' => 1, 'PDS' => 1 } ); foreach my $symbol (sort keys %{$extend_hash{6}}) { foreach my $exch (sort keys %{$extend_hash{6}{$symbol}}) { next if !$extend_hash{6}{$symbol}{$exch}{'specialist'}; next if !exists $spec_hash{$exch}{$symbol} || $spec_hash{$exch}{$symbol} == 1; print "$symbol = '$exch'\n"; } }

    If that doesn't do it for you, you need to actually ask a question and provide a run-able code sample. See I know what I mean. Why don't you? for some hints.

    Perl is the programming world's equivalent of English
Re: Use of uninitialized value
by tobyink (Canon) on Sep 10, 2014 at 07:26 UTC

    If the code works, the warning is annoying you, and the use of an uninitialized value on that particular line is actually expected, then just disable that warning. The "uninitialized" warning can be disabled using:

    no warnings "uninitialized";

    It is lexcially scoped, which means you can apply it to a whole file, or just a particular subroutine, or a single loop or if block.

    The point of the warnings pragma is that it's supposed to help you, not hinder you. If you find yourself jumping through hoops to keep it happy, then it's not doing its job - it's supposed to be keeping you happy, not the other way around. So just disable it for the scope where it's being annoying.

Re: Use of uninitialized value
by thewebsi (Scribe) on Sep 10, 2014 at 05:43 UTC

    You can try:
    if ( exists ( $spec_hash{$exch}{$symbol} ) and $spec_hash{$exch}{$symbol} != 1 ) {
    or
    if ( ($spec_hash{$exch}{$symbol}//0) != 1 ) {
    depending on how you want to handle the non-existing hash item.