in reply to Re: Hash Key Limitation problem
in thread Hash Key Limitation problem

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

Replies are listed 'Best First'.
Re^3: Hash Key Limitation problem
by lostjimmy (Chaplain) on Nov 14, 2008 at 16:59 UTC

    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"; } } } }