0

I'm close to get me crazy.

I have an array that seems to be ok. My array contains filenames (as keys) and full path on the values of the array. I checked that it's working.. up to here ok. Here is my code:

open (FILE, "comb_d.txt");
@l = <FILE>;
foreach $line (@l) {
    chomp($line);
    my @linea = split(/separator/,$line);
    $hash_d{$linea[0]} = $linea[1];
}

As I said.. it works, because I verified that:

foreach my $llave (keys %hash_o) {
    print "$llave = $hash_o{$llave}\n";
}

and it gives me full hash without issues..

Here comes the problem. I don't want to use all filenames (all keys) on my array, just a set of them. actually, there is a set of keys stored on @isect. But when I run:

foreach my $llave ( @isect ) {
    print "$llave = $hash_o{$llave}\n"; 
}

My result is:

filename1 = 
filename2 = 

I'm pretty sure that the elements on @isect exist as keys of %hash_o.

Help please

Thanks!!

TLP
  • 66,756
  • 10
  • 92
  • 149
alvgarci
  • 35
  • 6
  • 1
    Could you provide some lines from `comb_d.txt` and from `@isect` ? – Toto Feb 19 '13 at 15:13
  • `my @linea = split(/separator/,$line);` You sure you want `separator` and not `$separator`? – m0skit0 Feb 19 '13 at 15:16
  • 2
    What is the difference between `%hash_o` and `%hash_d`? – mob Feb 19 '13 at 15:23
  • 2
    You say that printing the key/values in `%hash_o` works fine, then you skip ahead to printing keys from an array `@isect`, but you don't show how you got those keys, or what the hash contains. How can we possibly help you then? – TLP Feb 19 '13 at 15:42
  • Remember to use `use strict;` and `use warnings;`. They may not solve this particular problem directly, but they'll save you some headaches in the future. As for the specific problem you're having, how are you populating `@isect`? Without more information, this is just a wild guess, but are you using `chomp` on those entries before they enter `@isect`? – Matt LaFave Feb 19 '13 at 15:53
  • @MattLaFave I assume those are his keys in the sample output he gives (filename1, filename2). – TLP Feb 19 '13 at 15:59

1 Answers1

1

For something like this, Data::Dumper is your friend.

use Data::Dumper qw<Dumper>;

...

    $hash_d{$linea[0]} = $linea[1];
    say Dumper( \%hash_d );
}

And then later:

say Dumper( \@isect );
say Dumper( \%hash_o );

You're spending a lot of time doing what Data::Dumper does for a light snack. And with each attempt at trying to do this, because it's Perl code and because you may not be up to speed with Perl, you're spending time debugging your debug code, instead of getting a solution done.

NOTE: Dumping is not an acceptable replacement for reading the API of an object, in Object-oriented Perl, as I explain here. But especially with unblessed hashes (see bless or perlobj), where it's just structured data that you want to know how to get at--it's the way to go.

Finally, here's where I mention that my favorite tool is Smart::Comments. Where you do this:

use Smart::Comments;
...
### %hash_d
### @isect
### %hash_o

And watch the magic.

Community
  • 1
  • 1
Axeman
  • 29,660
  • 2
  • 47
  • 102