in reply to hash of hashes


As per my views, I would like to tell you my suggestion is:

At the end, you are reading an input file call "EXAMPLE", and fetching each line by line, while doing so, In the code, $id = $_; what happens is $_ is the default variable which carries a trailing record separator from a string, so you have to remove that by using chomp function. that will work.


Don't put off till tomorrow, what you can do today.

Replies are listed 'Best First'.
Re^2: hash of hashes
by Anonymous Monk on Nov 23, 2006 at 11:27 UTC
    this is my complete code:
    use File::Find; find(\&wanted, 'upload_profile/'); sub wanted { #/\_$refine/ && print "$File::Find::name\n" && push @a +rray, $_; /predictions\_\Q$refine\E/i && push @found, $_; } ####sort array of foud files### @find = sort { $a cmp $b } @found; #print "find: @find"; foreach my $file (@find) { print "file: $file\n"; open (LOOKUP, "<upload_profile/$file") or die $!; while (<LOOKUP>) { ($name, $id) = (split m{\t})[3, 4]; print "id: $id\n"; $data{$file}{$id} = $name; } close (LOOKUP); } close (LOOKUP); open (dig_go, "<check.txt"); while (<dig_go>) { ($id, $name, $file_disappeared, $enzyme_one, $target_one, $enzyme_two, + $target_two, $enzyme_three, $target_three, $enzyme_four, $target_fou +r, $enzyme_five, $target_five) = split (m/\t/); ##tried chomp on each if (exists $data{$file_disappeared}{$id}) {print "match\n";} }
      this is my complete code:

      Well, what happened to  $refine =1; ? Where's the  use strict; and the  use Data::Dumper; that others suggested earlier? (In other words, if you think this is the whole script, you're wrong.) When you do this:

      while (<LOOKUP>) { ($name, $id) = (split m{\t})[3, 4]; print "id: $id\n"; $data{$file}{$id} = $name; }
      is it possible that the "$id" string (fifth tab-delimited field on each line of $file) is at the end of the line? If so, that would mean that there's a "\n" (or "\r\n") included in the value of $id.

      If you've already tried chop and chomp to no avail, try this instead:

      That can be handy for cases where you are reading data files that come from some OS other than the one your script is running on. Do that both inside the  while <LOOKUP> loop and inside the  while <dig_go> loop.

      Apart from that, if you'll start applying some of the advice you've already been given (use strict, use Data::Dumper), problems will be easier to diagnose, and you should be able to find the problem on your own. You might also want to use the perl debugger (start your script with  perl -d script_file), set break points inside each of those while loops, and see what's going on (cf. perldebtut and perldebug).

      If your script has  use Data::Dumper;, you can say  p Dumper(\%data) as a debugger command, to see what's in your hash structure, as well as just  p to print the current value of $_ (whatever was just read from a file). Very handy, and really simple. Do that.

      check.txt looks llike:
      1055 something test_predictions_1 info 1010 0 0 + 0 0 0 0 0 0