Let me reformat your code a little bit in order to make its purpose clear:
#!/usr/bin/perl -w use strict; my %myhash=(); my $myhash; open(DATA_LIST, "<data_list") or die "Can't open data_list to read!: $ +!\n"; while (<DATA_LIST>){ chomp; my ($Line,$Filed,$Dvalue)=split/\t/; #open(MYHASH, ">myhash") or die "Can't open myhash to write : $!\n"; # vladb: reading data from file 'file_data' into %myhash hash.. do_hash("file_data"); # vladb: are you absolutely sure value stored in $Line # was also found in the 'file_data' file which was used to # initialize the %myhash hash structure? print $myhash{$Line},"\n"; # *** vladb: move this line inside the f +ollowing if(..){} block. if (exists $myhash{$Line}){ print $Filed,"\t ",$Dvalue,"\t", $myhash{$Line},"\n"; } } sub do_hash { my ($filename)=@_; open(FH, $filename) or die "Can't open $filename: $!\n"; while(<FH>){ chomp; my ($Name, $Data)=split/\t/; $myhash{$Name}=$Data; } close FH; }
Please refer to my comments inside your code. The problem here, essentially, is that you are reading records from 'file_data' file and storing them in a global %myhash hash structure such that the first field of each record is your hash key, while the second field is your value. However, as you read records (lines) from the 'data_list' file and use the first field value (stored in the $Line) variable to reference a value in the %myhash hash, there's no absolute guarantee that the hash contains a matching key in the first place. One suggestion would be to move the print $myhash{$Line},"\n"; line inside the if block that follows. This way, you'll never attempt to access a hash record via a non-existant key.

UPDATE: Dear, Anonymous Monk you have to absolutely check for a key to exist in a hash before actually using it to retrieve any data from that hash. Therefore, to avoid the kind of error you were getting, my suggestion would be to move the line marked with '***' inside your if block where you check for the key to 'exist' before using it...

_____________________
$"=q;grep;;$,=q"grep";for(`find . -name ".saves*~"`){s;$/;;;/(.*-(\d+) +-.*)$/;$_=&#91"ps -e -o pid | "," $2 | "," -v "," "&#93;`@$_`?{print" ++ $1"}:{print"- $1"}&&`rm $1`;print"\n";}

In reply to Re: hash problem by vladb
in thread uninitialized value on hash lookup (was: hash problem) by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.