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

I'm writing a script which adds a string in records in one file, based on the presence of a part number in a second file. Basically, I search the first file, and whenever I find the flag I want, I add it to a hash like so:

$part_file{$pf_columns[0]} = 'Y';

$pf_columns[0] contains a string with the part number. Later on, I iterate over the lines of the second file. I grab the part number, and do something like this:

if (exists $part_file{$partnum}) { do_this(); }


The problem is, the if() block never runs. If I replace the exists with defined it still doesn't work. Now, if I replace the $part_file{$partnum} with $part_file{"SOME_PART_NO"} it works fine, for every line of the file. If I print out keys %part_file, then all the part numbers are there.

What am I missing here?

Thanks,
--J

Replies are listed 'Best First'.
Re: Odd Hash behavior
by davido (Cardinal) on Apr 14, 2004 at 21:57 UTC
    Just to satisfy curiosity and possibly detect a bug, try this:

    Assuming that you know that your hash keys are supposed to contain only [A-Za-z0-9] you could make non-A-Za-z0-9 characters visible to detect them.

    foreach my $key ( keys %part_file ) { $key =~ s/[^A-Za-z0-9]/Junk alert!/g; print "Key-$key-\n"; }

    You would then expect your output to be something like:

    Key-P123- Key-P334- Key-P925-

    But on the other hand, if you get something like:

    Key-P123Junk alert!Junk alert!- Key-P334Junk alert!

    ....then you'll be well on your way toward understanding why some of the other responses to your question advised you to be sure you're using chomp, or to watch for exterraneous characters that you didn't think were there.


    Dave

Re: Odd Hash behavior
by McMahon (Chaplain) on Apr 14, 2004 at 20:17 UTC
    chomp $partnum maybe?
Re: Odd Hash behavior
by Anonymous Monk on Apr 14, 2004 at 20:18 UTC
Re: Odd Hash behavior
by tedrek (Pilgrim) on Apr 14, 2004 at 20:18 UTC

    Have you checked that $partnum contains what you expect it to?

      Yes. I verified, using print statements, that the partnumber is the same both when I add it to the hash and when I look for the existence of the hash. Also, when I print out the keys, the part number is there in all its glory.

      If it helps, I'm using cygwin and perl 5.8.0.

      Also, I verified that there is no CR/LF (I'm presuming this is why people are suggesting chomp()).
        Rather than using simple print statements, do:
        use Data::Dumper; $Data::Dumper::Terse = 1; $Data::Dumper::Useqq = 1; ... print "partnum: ", Dumper($partnum), "\nkeys:\n", Dumper(keys %part_fi +le);

        Does the part number key contain leading/trailing gunk?

        Data::Dumper and/or placing leading and trailing sigils in your print statements can help detect unexpected whitespace.

        Hanlon's Razor - "Never attribute to malice that which can be adequately explained by stupidity"

        It sounds like $partnum is not the same, perhaps the case is different? It would help if we had a sample to look at as the code you've given doesn't seem to have any errors.

        Yes the possible presence of a CR/LF is why ppl are suggesting chomp()