If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Perl and the DBI > Compare 2 datafiles. Search and Update

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-22-05, 01:30
cojksmith cojksmith is offline
Registered User
 
Join Date: Feb 2005
Posts: 3
Compare 2 datafiles. Search and Update

I have 2 files.

File1 is id|date.

File2 is name|address|city|state|zip|phone|status|id.

I want to compare the 2 files. I want to read File1 "id" and find the match in File2 "id". If match found in File2 then I want to update status field in File 2 to Active.

Example data:

File 1---
1234|2/25/2005
8897|2/21/2004
9999|1/23/2001
0000|2/25/2006

File 2---
Lisa Smith|123 Main St|Baltimore|MD|99999|222-222-2222|Inactive|8897
Mindy Tye|897 Your St|New York|NY|99999|123-456-7822|Active|0000


The updated File2 should read:
Lisa Smith|123 Main St|Baltimore|MD|99999|222-222-2222|Active|8897
Mindy Tye|897 Your St|New York|NY|99999|123-456-7822|Active|0000


Basically, I want to use File1 to search File2 and do an update if a match is found.

Thanks for your help!
Reply With Quote
  #2 (permalink)  
Old 02-22-05, 06:20
reneeb reneeb is offline
Registered User
 
Join Date: Jan 2004
Location: Germany
Posts: 167
Try this one:
Code:
#! /usr/bin/perl

use strict;
use warnings;
use Tie::File;

my $file_one = 'file1.dat';
my $file_two = 'file2.dat';

my @ids;

open(FILE1,"<$file_one") or die $!;
while(<FILE1>){
  chomp;
  push(@ids,(split(/\|/,$_))[0]);
}
close FILE1;

tie my @array,'Tie::File',$file_two or die $!;
foreach my $line(@array){
  chomp $line;
  my @fields = split/\|/,$line;
  if(grep{$fields[$#fields] == $_}@ids){
    $fields[-2] = 'Active';
    $line = join("|",@fields);
  }
}
untie @array;
This code is untested!
__________________
board.perl-community.de - The German Perl-Community
Reply With Quote
  #3 (permalink)  
Old 02-22-05, 13:48
cojksmith cojksmith is offline
Registered User
 
Join Date: Feb 2005
Posts: 3
Still Need Help

My server doesn't have any of the cgi scripts this code requires.

Can you still help me with a basic cgi script?

Much appreciated, Thanks!
Reply With Quote
  #4 (permalink)  
Old 02-23-05, 05:02
reneeb reneeb is offline
Registered User
 
Join Date: Jan 2004
Location: Germany
Posts: 167
This is no CGI-Skript, but a script for command-line...
And this code requires no CGI-Skript, but three modules. Which version perl are you using? (type "perl -v" on the command line).

These three required modules are standard modules of perl.
If you have a perlversion lower than 5.6.x I would recommend to upgrade!

But without any modules, test:
Code:
#! /usr/bin/perl

my $file_one = 'file1.dat';
my $file_two = 'file2.dat';

my @ids;

open(FILE1,"<$file_one") or die $!;
while(<FILE1>){
  chomp;
  push(@ids,(split(/\|/,$_))[0]);
}
close FILE1;

my @array;
open(FILE2,"<$file_two") or die $!;
foreach my $line(<FILE2>){
  chomp $line;
  my @fields = split/\|/,$line;
  if(grep{$fields[$#fields] == $_}@ids){
    $fields[-2] = 'Active';
    $line = join("|",@fields);
  }
  push(@array,$line);
}
close FILE2;

open(WRITE,">$file_two") or die $!;
print $_,"\n" for(@array);
close WRITE;
__________________
board.perl-community.de - The German Perl-Community
Reply With Quote
  #5 (permalink)  
Old 02-23-05, 10:17
cojksmith cojksmith is offline
Registered User
 
Join Date: Feb 2005
Posts: 3
Thanks!

Thanks for your help!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On