Thanks Shawn, due to head injuries in a car crash, if I don't do something (as 
involved as this) every single day I forget all I've learned.
I did have examples to work from but they were lost in a system crash that 
totalled my hard drive.
Having read back my op, maybe I need to tidy up what I'm asking :-)
File 1 will only have 2 comma separated values to deal with, whilst file 2 will 
have 3 or more, so I take it this would be entering the realms of arrays?
I don't mind if the output goes directly to file 2, but as it is so easy to do 
a 
simple command line copy of one file to another, maybe for the sake of sanity 
it 
might be better to handle both input files and output to a 3rd work file, which 
can be renamed later.

Also, what would be the best parms to open the necessary input files?

thanks
Brian




________________________________
From: shawn wilson <ag4ve...@gmail.com>
To: Jim Gibson <jimsgib...@gmail.com>
Cc: beginners@perl.org
Sent: Mon, November 1, 2010 9:07:36 AM
Subject: Re: Compare files

I personally don't under #3. I would do something like:

while( my ($key, $value) = each %file2) {
print "$key,$value" if( !defined( $file1{ $key } ) );
print "$key,$file1{ $key } if( defined( $file1{ $key } ) );
}
That assumes that the keys are exact between files.

However, as I wrote this, I think what you were saying is to pop off the
values as you print and then loop the remaining?
On Nov 1, 2010 2:46 AM, "Jim Gibson" <jimsgib...@gmail.com> wrote:
> At 9:34 PM +0000 10/31/10, Brian wrote:
>>Thanks for the previous help, that triggered a few dormant grey cells :-)
>>and leads me to another question.
>>
>>I would like to compare 2 (unsorted) csv files..
>>
>>file 1 contains
>>
>>fredbloggs,0
>>joebloggs,3
>>joeblow,6
>>
>>file 2
>>
>>fredbloggs,1
>>joebloggs,4
>>
>>replace the value in file 2 with the value in file 1.
>>if the item in file 1 doesn't exist in file 2, insert/append the line
>>
>>so that
>>
>>file 2 becomes
>>
>>fredbloggs,0
>>joebloggs,4
>>joeblow,6
>
> How big are your files? If they are not too big, I would do the following:
>
> 1. Read file 1, remove the end-of-line (EOL) character from each
> line, split the line into two strings, save the two strings in a hash.
> 2. Read file 2, remove the EOL, split the line, check to see if the
> key exists in the hash and, if it does, replace the second field with
> the corresponding hash value. write out the line, and (here is the
> tricky part) delete the hash entry.
>
> 3. Iterate over the hash entries that remain and add them to the end
> of the file.
>
> Note: this will only work if the key values in file 1 and 2 are unique.
>
> Can you write the code for each of these steps?
>
> For information on the Perl functions you need, see the following:
>
> perldoc -f open
> perldoc -f chomp
> perldoc -f split
> perldoc -f exists
> perldoc -f delete
>
> --
> Jim Gibson
> j...@gibson.org
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>



      

Reply via email to