roja12345 0 Newbie Poster

Hi Perl guru's

I'm new on Perl i need help for below mentioned issue.

I have 4 flat files with 4 columns like below:
eg:

000360112720C17506721111130602 m     1L 1008968860482

I have one excel file with 12 columns
I have converted the excel file into text with comma seperated.

Now i need to compare the converted text files few columns values with 4 flat files if the values matches then i have replace the string or space with chracter "N" in 4 flat files.

Or write log file as the data mismatch. Pls help me. Below is my code pls guide me how to do it?

#!/usr/bin/perl -w
use strict;
use Data::Dumper;
use Text::ParseWords;

my ($dataFile1, $dataFile2) = ("data.txt", "data1.txt");
open FILE1, "<" . $dataFile1;    
open FILE2, "<" . $dataFile2;

my (@textFile, @excelFile, @excelLine, @missing, @avaliable);
my ($check_line, $split_line, $excelPCode, $excelSplit, $excelDma, $z, $k, $l, $arrayCount);
my ($field1, $field2, $field3, $field4, $field5, $field6, $field7, $field8, $field9, $field10, $field11, $field12, $field13, $field14, $field15 );
my  $i           = 0;
my  $j           = 0;
my  $v           = 0;
my  $productCode = "",
my  $zipCode     = "",
my  $dmaCode     = "",
my  $salesValue  = "",

my (%flatResults, %excelResults, %excelSplit, %test);

# Flat File Content Parsing
while (<FILE1>)
{
  chomp;        
  #push @textFile, split /\s+/, $_; 

  ($field1, $field2, $field3, $field4) = split /\s+/, $_;

    #Pick the Product Code from Flat File  
    $productCode = substr($field4, -11);              

    #Pick the ZipCode Code from Flat File
    $zipCode = substr($field1, 14, -11);    

    #Pick the DMA from Flat File        
    $dmaCode = substr($field1, -2);   

    #Pick the DMA from Flat File        
    $salesValue = "";   

    # Prepared a hash with required values      
    %{$flatResults{$i}} =
    (
        "PCode"   => $productCode,
        "ZipCode" => $zipCode,
        "DmaCode" => $dmaCode   
    );

    #%{$flatResults{$i}} =
    #(
    #   "PCode"   => $productCode,
    #   "ZipCode" => $zipCode,
    #   "DmaCode" => $dmaCode,
    #   "Sales"   => $salesValue        
    #);
    $i++;   
 }

close(FILE1);

# Excel File Content Parsing
while (<FILE2>)
{
    chomp;      
    #push @excelFile, split /\,+/, $_; 
    #push @excelFile, $_;   
    ($field1, $field2, $field3, $field4, $field5, $field6, $field7, $field8, $field9, $field10, $field11, $field12, $field13, $field14, $field15 ) = split /\,+/, $_;

     if($j!=0)
     {
        #Praper the Product Code from Excel File
        if(length($field10)==12)
        {
            $excelPCode = substr($field10, 0, 11); 
        }
        else
        {
            $excelPCode = substr($field10, 1, 11); 
        }

        #Praper the DMA Code from Excel File
        if(length($field8)<2)
        { 
            $excelDma = substr("00000$field8",-2); 
        }
        else    
        {
            $excelDma = $field8; 
        }

        %{$excelResults{$j}} =
        (
            "PCode"   => $excelPCode,
            "ZipCode" => $field7,
            "DmaCode" => $excelDma
        );

        #%{$excelResults{$j}} =
        #(
        #   "PCode"   => $excelPCode,
        #   "ZipCode" => $field7,
        #   "DmaCode" => $excelDma,
        #   "Sales"   => $field13
        #);

     }      
    $j++;   
 }
close(FILE2);

#Compare the Excel data with the Flat file records
 for($z = 0; $z< scalar(keys(%flatResults )); $z++)
 {
    #Prepare a excel loop count ignore the first records
    for($k = 1; $k<= scalar(keys(%excelResults)); $k++)
    {       
                # if matches the replace string with "N" section    
        if(($flatResults{$z}{'PCode'} == $excelResults{$k}{'PCode'}) && ($flatResults{$z}{'ZipCode'} == $excelResults{$k}{'ZipCode'}) && ($flatResults{$z}{'DmaCode'} == $excelResults{$k}{'DmaCode'}))
        {
            #print "flatResults{$z}{'PCode'}==>".$flatResults{$z}{'PCode'}."--excelResults{$k}{'PCode'}==>".$excelResults{$k}{'PCode'}."\n";
            print $excelResults{$k}{'PCode'}." is Equal\n";

        }
        else
        {   # if not match write log file.      
            #print "flatResults{$z}{'PCode'}==>".$flatResults{$z}{'PCode'}."--excelResults{$k}{'PCode'}==>".$excelResults{$k}{'PCode'}."\n";
            print $excelResults{$k}{'PCode'}." is Not Equal\n";

                # Create and Write to missing Log file 
                open LOG, ">>missingDetailsLog.txt";
                print LOG "[$day-$month:$yr19] Product Code : $excelResults{$k}{'PCode'}  Zip Code : $excelResults{$k}{'ZipCode'}  DMA Code : $excelResults{$k}{'DmaCode'} are not match with any record in $dataFile1\n";
                close LOG;
            }
        }
    }
 }

Please help me in this!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.