Help - Search - Member List - Calendar
Full Version: update file
WorkTheWeb Forums > Webmaster Resources > Perl Beginner Help
Support our Sponsors!
Rafael Morales
Hi list.

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

As you can see the first column is different so I need to update the second file for both files have the same values, how could I do it ???
Note: My comparison works fine, I just need update it.

Thanks and regards !!!

--
_______________________________________________
Get your free email from http://mymail.bsdmail.com

Wiggins d'Anconia
Rafael Morales wrote:
QUOTE
Hi list.

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

As you can see the first column is different so I need to update the second file for both files have the same values, how could I do it ???
Note: My comparison works fine, I just need update it.

Thanks and regards !!!


What have you tried? Where did it fail? Show us some code.

perldoc perlopentut
perldoc Tie::File
perldoc DBD::CSV

http://danconia.org

Rafael Morales
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;

my file1 = "path to file";
my file2 = "path to file";

open(FILE1, "<$file1") || die; # just read
open(FILE2, "+<$file2") || die; # for update it

foreach $nomina (<FILE1>)
{
my $match = 0;
my @nomina = split /|/, $nomina;

foreach $bucareli (<FILE2>)
{
@bucareli = split /|/, $bucareli;

if ( $nomina[3] == $bucareli[3] )
{
if ( $nomina[0] == $bucareli[0] )
{
$match = 1;
}
}
}

if ( $match == 1 )
{
print FILE2 "$nomina[0]n";
}
}


----- Original Message -----
From: "Wiggins d'Anconia" <[Email Removed]>
To: "Rafael Morales" <[Email Removed]>
Subject: Re: update file
Date: Thu, 14 Jul 2005 10:56:54 -0600

QUOTE

Rafael Morales wrote:
Hi list.

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

As you can see the first column is different so I need to update
the second file for both files have the same values, how could I
do it ???
Note: My comparison works fine, I just need update it.

Thanks and regards !!!


What have you tried? Where did it fail? Show us some code.

perldoc perlopentut
perldoc Tie::File
perldoc DBD::CSV

http://danconia.org

--
To unsubscribe, e-mail: [Email Removed]
For additional commands, e-mail: [Email Removed]
<http://learn.perl.org/> <http://learn.perl.org/first-response


--
_______________________________________________
Get your free email from http://mymail.bsdmail.com

Wiggins d'Anconia
"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:
QUOTE
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.

QUOTE

my file1 = "path to file";
my file2 = "path to file";


The above two lines are missing their sigils.

my $file1 = ...;
my $file2 = ...;

QUOTE
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";

QUOTE
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.

QUOTE
{
my $match = 0;
my @nomina = split /|/, $nomina;


You did not backslash the | here. Consistency.

QUOTE
foreach $bucareli (<FILE2>)

You did not declare $bucareli. See above comment about foreach.

QUOTE
{
@bucareli = split /|/, $bucareli;

Missing declaration.

QUOTE

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

QUOTE
$match = 1;
}
}
}

if ( $match == 1 )
{
print FILE2 "$nomina[0]n";
}
}

[snip]


QUOTE
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



PHP Help | Linux Help | Web Hosting | Reseller Hosting | SSL Hosting
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2006 Invision Power Services, Inc.