Chern Jian Leaw wrote:
> 
> Hi,

Hello,

> I have a script written below (by John Krahn and WC-Sx-Jones from this
> mailing list) which changes the file format from:
> #cat PSCS-ORIG
> abinabdu adanie2  agibson  agoh1    aiabouse
> akko     alau     alee1    alee2    amitb
> amohdali amshams  anmohand
> 
> [snip]
> 
> I have another file(employeeID.txt) which having the format:
> 1066:hsridhar
> 7937:ssmn
> 7979:kpushpar
> (output truncated  ... )
> 
> where the numbers 1066 - 7979 represents the user's employee ID.
> Then names [hsridhar - kpushpar] represents the usernames themselves.
> 
> I would like to loop through the employeeID.txt file, but using the
> hash keys created in the earlier script as the keys to retrieve the
> employeeID corresponding to the usernames in the hash key. Note that
> the names appearing in the hash may or may not appear in the
> employeeID.txt file.
> 
> My problem:
> I'm not sure of how I could insert the employeeID values correponding
> to the key values from the %count hash(used during the conversion of
> the file format), then search and print the corresponding employeeID
> based on the key values (usernames) from %count hash.

If I understand your problem correctly then you may want something like
this:

#!/usr/bin/perl
use warnings;
use strict;

my $empl_file = '/home/data/employeeID.txt';
my $file      = 'PSCS-ORIG';

open FILE, $empl_file or die "Cannot open $empl_file: $!";
my %employees = reverse map /^(\d+):(\S+)/, <FILE>;
close FILE;

open INFILE, $file or die "Cannot open $file: $!";
while ( <INFILE> ) {
    for ( split ) {
        if ( exists $employees{ $_ } ) {
            print "Here, $_ is $employees{$_}\n";
            }
        else {
            print "No such element $_\n";
            }
        }
    }
close INFILE;

__END__



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to