Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2005
    Posts
    3

    Unanswered: 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!

  2. #2
    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

  3. #3
    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!

  4. #4
    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

  5. #5
    Join Date
    Feb 2005
    Posts
    3

    Thanks!

    Thanks for your help!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •