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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Hash Key Limitation problem
by lostjimmy (Chaplain) on Nov 06, 2008 at 16:42 UTC
    Have you even taken a look at the advise given in your other node (Multiple Hash key values problem !!)? Specifically, the replies from gwadej and repellent. This code only requires a couple of small changes.
    1. Instead of $href{$1} = $2 if $_ =~ /(\S+)\s+(\S+)/;, you need to use a hash of arrays and push the new value into the array ref associated with that key.
    2. Your second while loop is going to need a nested loop within. After splitting the numbers, loop over each number in @{$href{$name}} and check for the condition.
    Trying out these suggestions, as well as those given by others in your other post should get you to where you need to be.
      Please check my modified code but i am unable to retrieve back my hash array values and thus an error :( I am using Hash array for the first time actually

        You're so close to getting this. The main problems with your code are my $one_number = $numbers[${$href{$name}} - 1]; and print "$name\t\t${$href{$name}}\t$one_number\n"; You're having some trouble with derefencing the arrays (and also with choosing the "one number"). The correct syntax would be $numbers[$href{$name}->[0]] (assuming you want the first element), or $numbers[${$href{$name}}[0]].

        After seeing your updates, I think I'm a bit confused about exactly what you're trying to accomplish now. I had initially thought that you wanted to match each value in the array against every line in File2. If that is still the case, you need to add a nested loop.

        Anyway, here is some updated code. I hope this does what you need it to do.

        #!/usr/bin/perl -w use strict; use warnings; my %href; open my $fh, "<", "File1.txt" or die "Cannot open File1: $!"; while (<$fh>) { chomp; push @{$href{$1}}, $2 if /(\S+)\s+(\S+)/; } while (my ($key, $value) = each(%href)) { print "$key, (@{$value})\n"; } close $fh; open $fh, "<", "File2.txt" or die "Cannot open File2: $!"; { local $/ = '>'; while ( <$fh> ) { chomp; next unless ( s{ \A (\S+) \s+ (?= \d ) }{}xms and exists( $hre +f{$1} )); my $name = $1; my @numbers = split /\D+/; for my $index (@{$href{$name}}) { next if $index > $#numbers; if ($numbers[$index] >= 20) { print "$name\t\t$index\t$numbers[$index]\n"; } } } }