in reply to compare 2 files and print to a third

I took a liberty and rephrased your question. I hope I got it right.

I am wondering how to take 2 different files whose lines have a unique key called $login. This key is at the beginning of the line and it is the only piece of information common to the records. I would like to go through the file, line by line, and concatenate the contents of all the lines with the same keys, which I will output to a third file.

To do this, you will want to read in both files, and isolate $login and use it as a hash key. (So far, so good.) So use a regular expression or split (or something) and do

if ( exists ( $hash{$login} ) ) { $hash{$login} .= $delimiter . $_; } else { $hash{$login} = $_; }

Then, once you are done, you will want to print out your hash.

foreach my $key (keys %hash) { print "$key : $hash{$key}; }

Replies are listed 'Best First'.
Re: Re: compare 2 files and print to a third
by BrowserUk (Patriarch) on Nov 06, 2003 at 00:38 UTC

    Personally, I would favor

    File 1

    key1 other stuff key2 other stuff ...

    File 2

    key2 more stuff key1 more stuff ...

    Desired result: File3

    key1 other stuff more stuff key2 other stuff more stuff

    I tried this

    ....

    but it's not working because ....


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    Hooray!
    Wanted!