Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: searching two files

by ikegami (Patriarch)
on Dec 22, 2004 at 21:19 UTC ( [id://416908]=note: print w/replies, xml ) Need Help??


in reply to searching two files

First, you need to read up on how to read files. Second, when you want to your code to look something up -- you want to lookup if a name is in the other file -- think of hashes. The following should be useful.

use strict; use warnings; my %fileA; my %fileB; my %users; # Read in FileA. open(A, "FileA") || die "$!\n"; while (<A>) { my ($name, $machine, $laston) = split(/,/, $_); $fileA{$name} = [ $machine, $laston ]; } close(A); # Read in FileB. open(B, "FileB") || die "$!\n"; while (<B>) { my ($name, $password, $location) = split(/,/, $_); $fileB{$name} = [ $password, $location ]; } close(B); # Check for users missing in FileB. # Merge records found in both FileA and FileB. foreach my $name (keys(%fileA)) { if (!exists($fileB{$name})) { warn("$name was found in file FileA, but not in file FileB.\n"); next; } $users{$name} = [ $fileA{$name}, $fileB{$name} ]; #delete($fileA{'name'}); # not needed delete($fileB{'name'}); } # Check for users missing in FileA. foreach my $name (keys(%fileB)) { if (!exists($fileA{$name})) { warn("$name was found in file FileB, but not in file FileA.\n"); } } # Do something with users. # Specifically, display matches. foreach my $name (sort keys(%users)) { printf("user %s\n". "machine: %s\n". "last on: %s\n". "password: %s\n". "location: %s\n". "\n", @{$users{$name}} ); }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://416908]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-04-25 14:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found