in reply to Help to identify Error in my program

As people have said is because your keys don't exists.
I've neatened up your code a bit, to make it more readable.
Some pointers:
- use English: This lets you use $INPUT_RECORD_SEPARATOR instead of $\ or $/ I can't even remember which one, I only knew from your input file
- use Data::Dumper: This lets you dump variables to see what's inside them
- use lowercase keys (lc) this ensures case isn't an issue


The output I get is:
john 10 50
mathew 27 40


Code:
#!/usr/bin/perl use warnings; use English q/-no_match_vars/; use strict; use Carp qw/croak carp/; use Data::Dumper; my $file_one_ref; my $file_one_fn = shift or croak "No input file specifed for file one" +; my $file_two_fn = shift or croak "No input file specifed for file two" +; open my $file_one_fh, '<', $file_one_fn or croak "Could not open filename [$file_one_fn] : ", $OS_ERROR; while (<$file_one_fh>) { $file_one_ref->{ lc $1 } = $2 if / \A (\w+) \s+ (\w+) /xms; } # You can do the above input like this but it's not as efficient: # map { $file_one_ref->{ lc $1 } = $2 if / (\w+) \s+ (\w+)/xms; } <$in +put_fh>; # Have a look at the keys read in: print Dumper($file_one_ref), "\n\n"; open my $file_two_fh, '<', $file_two_fn or croak "Could not open filename [$file_two_fn] : ", $OS_ERROR; $INPUT_RECORD_SEPARATOR = '>'; while ( my $line = <$file_two_fh> ) { # I reserve s{}{}xms; for multi line expressions next if ( $line !~ s/ \A (\S+) \s+ (?= \d ) //xms and !exists $fil +e_one_ref->{ lc $1 } ); # why not just use a match? is $name going to contain numbers my $name = lc $1; my $numbers = [ grep { $_ } split /\D+/, $line ]; my $one_number = $numbers->[ $file_one_ref->{$name} - 1 ] || next; if ( $one_number >= 20 ) { print join " ", $name, $file_one_ref->{$name}, $one_number, "\ +n"; } } close $file_one_fh or croak "Could not open filename [$file_one_fn] : ", $OS_ERROR; close $file_two_fh or croak "Could not open filename [$file_two_fn] : ", $OS_ERROR; __END__
Update: See below.

Replies are listed 'Best First'.
Re^2: Help to identify Error in my program
by Fletch (Bishop) on Oct 21, 2008 at 14:26 UTC

    If you're going to use English and aren't actually using the verbose forms of $& and friends it's better to use English '-no_match_vars'; instead so as not to slow down every regex match your program does (presuming a post 5.8 perl; before that there's not a way to dodge the slowdown bullet).

    (But really, bite the bullet and learn which is which; or just remember: perldoc perlvar is my friend.)

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

      Thanks, Always learning.
      John