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