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/ > >