"Because it's up-side down. Why is that? It makes replies harder to read. Why not? Please don't top-post." - Sherm Pendley, Mac OS X list
Rafael Morales wrote: > This the code I use for compare, and works, but I don't know how to update > the file2, I hope you understand me > > #!/usr/bin/perl > use strict; Do you really have 'use strict' in your file? If so there are problems below that you haven't corrected and your script won't run. If this is a chunk of a larger script you should tell us so that we don't think this is complete. > > my file1 = "path to file"; > my file2 = "path to file"; > The above two lines are missing their sigils. my $file1 = ...; my $file2 = ...; > open(FILE1, "<$file1") || die; # just read > open(FILE2, "+<$file2") || die; # for update it > It usually helps to include an error message when calling die, and when that message is caused by an internal function usually you should include the special variable $! to indicate *why* it died. open(FILE1, "<$file1") || die "Can't open field for reading: $!\n"; > foreach $nomina (<FILE1>) You did not declare $nomina which is why I have the question about use strict. In general it is usually better to use a while loop instead of a foreach when reading files line by line. The foreach loop causes the whole file to be read in immediately and stored to memory. Using a while loop will read only a line at a time. > { > my $match = 0; > my @nomina = split /|/, $nomina; > You did not backslash the | here. Consistency. > foreach $bucareli (<FILE2>) You did not declare $bucareli. See above comment about foreach. > { > @bucareli = split /\|/, $bucareli; Missing declaration. > > if ( $nomina[3] == $bucareli[3] ) > { > if ( $nomina[0] == $bucareli[0] ) > { The above two checks can be condensed, and no real need to set a temporary variable. Just perform your match stuff right in the if block, if ($nomina[3] == $bucareli[3] and $nomina[0] == $bucareli[0]) { print FILE2 .....; Since you haven't stated clearly what the real problem is, I assume this is part of it. You are only printing the one column of the record back to the file. You need to re-join the elements of the line and print them back, updating the ones that have changed. For instance, print FILE2 join '|', $nomina[0], @bucareli[1..$#bucareli]; perldoc -f join Of course if you are really trying to make the files identical then there is certainly a much shorter way. http://danconia.org > $match = 1; > } > } > } > > if ( $match == 1 ) > { > print FILE2 "$nomina[0]\n"; > } > } > [snip] >>>I have two files which I compare: >>>first file: >>>813|42006|34913|373376|SALAZAR/BERLANGA/JUAN FRANCISCO >>>K00|42004|999999|489545|FAUSTO/PERALTA/PORFIRIO >>>900|72059|2031|237648|CAZARES/GUTIERREZ/ALEJANDRO >>>211|42005|34913|86258|GUZMAN/DIAZ/CARLOS NOE >>> >>>second file: >>>000|42006|34913|373376|SALAZAR/BERLANGA/JUAN FRANCISCO >>>111|42004|999999|489545|FAUSTO/PERALTA/PORFIRIO >>>222|72059|2031|237648|CAZARES/GUTIERREZ/ALEJANDRO >>>333|42005|34913|86258|GUZMAN/DIAZ/CARLOS NOE|20050609 >>> -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>