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

In reply to Re: searching two files by ikegami
in thread searching two files by tcf03

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.