Avoid duplicates in perl code

Need to exclude the duplicate stores in the output below.
code part 1

#!/usr/bin/perl -w -I/SCD/bin
use ret::ret qw(:all);
use SCD3;
use Date::Calc qw(:all);
use Time::Local;
use File::Copy;
# Set up base SCD path name
our $basepath = $ENV{SCDPATH}; #Get the Environment Variable for the $basepath
if (!defined $basepath){
$basepath = "/SCD";
}
# Set up location of compiled exacutables
our $BINPATH = "$basepath/bin";
# Set up location for shell scripts
our $SHELLPATH = "$basepath/shell";
our $PARMPATH = "$basepath/parm";
#   Send error mail (if applicable) and quit
sub end_processing{
my $email_address = "SCD3.dl\@ret.com" if (! defined $email_address);
  if(@user_message){
     open (MAIL, "|mailx -s \"SCD3 output for $chain_code $subchain_store\" $email_address");
     print MAIL @user_message;
     close MAIL or die "Unable to send mail for $chain\n";
     print "@user_message \n";
 }
 # Exit with success
 exit;
}
sub create_sub_directories{
  # Expect reference to list of directories
  my ($dir_list_ref) = @_;
  # For each directory on the list
  foreach $dirname (@$dir_list_ref) {
     # Create it as root if it doesn't exist
     system ("echo $BINPATH/mkdir -m 770 $dirname | newgrp SCD")   unless ( -d "$dirname");
  }
}
# Get the file pattern to be matched with the input file
sub get_sales_pattern{
  # If chain name is rama, pattern is nettoret.dat
  if ($chain eq "rama"){
   $file_pattern ="nettoret.DAT\$";
  }
  else
  { die "Invalid Chain name: $chain \n"; }
   return $file_pattern;
}
# Get the list of sales files requretng processing
sub get_file_list{
   # Expect chain name and subchain or store
   my ($chain) = @_;
   undef %filelist_hash; # Flush the hash
   # Read in list of files in input directory
   # abend program if fork fails
   open (FILELIST, "ls $basepath/TEMP/$chain |")or die "Cannot fork to ls: $!";
   # For each filename
   foreach $filename (<FILELIST>) {
     # Remove the trailing space
     chomp $filename;
     # Split into elements delimited by "."
     @file_elements = split (/\./, $filename);
     # Get the sales file pattern for this chain
     $file_pattern = &get_sales_pattern($chain);
     # Get the subchain or store from the filename
     $file_geog = shift(@file_elements);
     # Get the ret week from the filename
     $week_in_filename = shift(@file_elements);
     # Get the chain code from the filename
     $chain_code1 = shift(@file_elements);
     # Get the timestamp from the file name
     $timestamp = shift(@file_elements);
     # Drop the five digit component from the filename (confirm later)
     shift(@file_elements);
     # Reconstruct the raw filename
     $raw_filename = join("\.",@file_elements);
     $raw_filename = uc $raw_filename;
     # Store the filename if the first element is
     # input subchain or store and the sixth element
     # matches the sales file pattern retrieved above
     $filelist_hash{$week_in_filename}= $filename if ((uc $raw_filename =~ /$file_pattern/)
                                       && ($file_geog eq $subchain_stor
   }
    # die on ls failure
    close FILELIST or die "Cannot read input directory filelist: $!";
   # If no files found,abort processing
   # otherwise, return sorted list of filenames
   if(!defined (%filelist_hash)){
         push(@user_message, "No data to process for $chain\n");
         &end_processing;
   }
   return (\%filelist_hash);
}
#  Check that the store in the filename is valid for store processing       *
sub check_store
{
  # Requires chain code and store number
  my($chain_code, $store,$filelist_ref) = @_;
  # Pipe output of subchain lookup or abend
  open (SUBCHAIN, "$BINPATH/SCD3_subchain $store|")
        or die "Cannot fork for process SCD3_subchain\n";
  # Read all prog output
  @subchain_info = <SUBCHAIN>;
  # close the pipe
  close SUBCHAIN or die "Cannot retrieve subchain for store $store\n";
  # Get number of elements in list
  $subchain_length = @subchain_info;
  # If no data is returned abend
  die "No subchain returned from SCD3_subchain - aborting process\n"
        if $subchain_length == 0;
  # Run external process to get subchain
  $subchain = $subchain_info[0];
  chomp $subchain;
  # If subchain = 'XX' (invalid store)
  if ($subchain eq 'XX')
  {
    # For each input sales file
    foreach $file (@$filelist_ref)
    {
     # Issue rejected file warning
      push (@user_message, "Store $store for $chain does not exist or is inactive\n\n");
     # Copy file to REJECTED directory
      copy ("$basepath/TEMP/$chain/$file", "$chain_path/REJECTED/$file")
        or die "Cannot copy $basepath/TEMP/$chain/$file to REJECTED\n";
      &end_processing;
    }
   }
  # return the subchain
  return $subchain;
}
#  Store Validation
sub store_validation{
  my ($salesfile_hash_ref, $chain_code, $subchain, $chain, $units_to_weight)= @_;
  my $new_units_to_weight;
  my $units_parm_file = "$PARMPATH/units_to_grams.RTN";
  my $units_to_weight = &read_units_to_grams ( $units_parm_file );
  #Looping for every week
  WEEK: foreach $retweek (sort keys %{$salesfile_hash_ref})
  {
   #Get the name of the file for the given retweek
   my $file=$$salesfile_hash_ref{$retweek};
   my @missing_store_header1=();
   my $file_trailer_found= "false";
   my $xfound = "false";
   my $yfound = "false";
   my $num_file_trailer=0;
   my $closingdayerror="false";
   my $close_date=0;
   my @extra_file_header =();
   my @incomplete_store = ();
   my @invalid_record = ();
   my @incorrect_store_week = ();
   my @ndf = ();
   my $invalid_rec_cnt=0;
   my $linecount=0;
   my $total_lines=0;
   #my $record_count=0;
   my $store_num=0;
   my $count = 0;
   my $processing_week=$retweek;
   undef %extrastore_line;
   undef %extra_store;
   undef %wrongweek_line;
   undef %invalid_rec_count;
   undef %store_hash;
   open (SALES_FILE, "$basepath/TEMP/$chain/$file") or die "Can not open $file :$!\n";
   # Looping through every line of the file
   LINE:  while ($line=<SALES_FILE>){
    #my $record_count = 0;
    $linecount++;
    chomp $line;
    $line =~ s/\cM$//;
    #next if($line=~/^A/);
   #Check if the file header is missing
   if(`head -1 $basepath/TEMP/$chain/$file`!~/^A/) {
     push (@user_message,"No file header for rama $file: file rejected");
     &end_processing;
   }
   if($line =~/^A/)
   {
     # Check the relationship of the date in the header (A type) record to the processing week
     $creation_date = join("/",substr($line,13,2),substr($line,11,2),substr($line,9,2));
     # Calculate the week from the header record
     $header_week = ret_week($creation_date);
     if($header_week !~/\d{4}/){
       push(@user_message,"The week extracted from the header of the file is not 4 digits numeric\n");
       &end_processing;
     }
     if($header_week != $processing_week) {
       push(@user_message, "Week error for $chain in $file: expected week is $processing_week,file week is
                 $header_week.\nIf this is not what you intended, please investigate and reprocess\n");
      return AZBX;
     }
     $list_list_ref = ret_week($header_week);
     $list_ref = @$list_list_ref[1];
     $seconds_endretweek =    timelocal 0, 0, 0, @$list_ref[0], (@$list_ref[1]) - 1, @$list_ref[2];
     $seconds_creationdate =  timelocal 0, 0, 0, substr($line,13,2),substr($line,11,2)-1,substr($line,9,2);
     # Creation date must be on sa, su or mo. If creationdate is within 2 days off end of week (sa or su)then week is ok.
     $dayerror = "false";
     if (($seconds_endretweek - (1 * 24 * 60 * 60)) >= $seconds_creationdate){
       # Creation date is before saturday. This is a possible error.
       # Check If creation date is on monday then its ok else error.
       # When closingdate is on monday then the week is the previous week.
       if (($seconds_endretweek - (5 * 24 * 60 * 60)) >= $seconds_creationdate){
        $processing_week--;
       }
       else
       {
         # Postpone errormessage. This error can only popup if if no week error
         $dayerror = "true";
         push(@user_message,"Creation date on A record  is in the range Tuesday to Friday File :$file\n");
       }
     } # End of if (($seconds_endretweek - (1 * 24 * 60 * 60)) >= $seconds_creationdate)
     if ($processing_week eq 0000)
     {
       $week_now = ret_week(join "-", reverse(Today())) - 1;
       if ($header_week != $week_now)
       {
         push (@user_message, "Week error for $chain in $file: expected week $week_now,file week is $header_week
         \n Please investigate and submit manually\n");
         return AZBX;
       }
     } # End of if ($processing_week eq 0000)
   }  # End of if($line =~/^A/)
  if($line =~/^E/)
  {
     #$record_count++;
     $store_id1=substr($line,1,8);
     $store_header1{$store_id1}=1;
  }
  if($line =~/^I/)
  {
     #$record_count++;
     $store_id2=substr($line,1,8);
     #$store_header2{$store_id2}=1;
     #Check the relationship of the closing date in each I type record to the processing week
     $closing_date = join("/",substr($line,21,2),substr($line,19,2),substr($line,17,2));
     $close_date=substr($line,17,6);
     $closing_week = ret_week($closing_date);
     if($closing_week !~ /\d{4}/)
     {
       push(@user_message,"The closing week is not 4 digits numeric\n");
       &end_processing;
     }
     $list_list_ref = ret_week($closing_week);
     $list_ref = @$list_list_ref[1];
     $seconds_endretweek = timelocal 0, 0, 0, @$list_ref[0], (@$list_ref[1]) - 1, @$list_ref[2];
     $seconds_closing_date = timelocal 0, 0, 0, substr($line,21,2),substr($line,19,2)-1,substr($line,17,2);
    # Creation date must be on sa, su or mo. If creationdate is within 2 days off end of week (sa or su) then week is ok
    $closingdayerror = "false";
    if (($seconds_endretweek - (1 * 24 * 60 * 60)) >= $seconds_closing_date)
    {
          #Creation date is before saterday. This is a possible error check
          #If creation date is on monday then its ok else error
          #When closingdate is on monday then the week is the previous week.
         if (($seconds_endretweek - (5 * 24 * 60 * 60)) >= $seconds_closing_date)
         {
           $processing_week--;
         }
         else
         {
          $closingdayerror ="true";
         }
  }
 }
if(!exists $store_header1{$store_id1})
 {
     push(@{$extrastore_line{$retweek}},"$line\n")unless($line =~ /^E/);
     push(@missing_store_header1,"$line\n")if($line =~ /^I/);
 }
 if($line=~/^J/)
 {
    #$record_count++;
    $store_num=substr($line,1,2);
 }
 # If the closing date does not equal either the end date of the processing week, end date plus 1, or end date plus
 # 2 (ie Saturday of the processing week or Sunday/Monday of the following week), then all E, I, J, P, Q and X type records   # for the store should be written as they stand to /basedir/CHAINS/rama/REJECTED/weeks.wwww.tmp
 if($closingdayerror eq "true")
 {
    push(@{$wrongweek_line{$retweek}},"$line\n")unless($line =~/^E/);
    push(@incorrect_store_week,"Store $storecat_ok{$store_num}($store_num)  Close date $close_date \n");
 }

 #Check for Extra store
 if (!defined ($storecat_ok{$store_num}))
 {
      $extra_store{$store_num}=1;
      push(@{$extrastore_line{$retweek}},"$line\n")unless($line =~/^E/);
 }
 else
 {
  $valid_store{$store_num}.="$line~" unless($line =~/^E/);
 }

 foreach $store(sort keys %valid_store)
 {
  if(!defined $store_hash{$store})
  {
    $store_hash{$store}=1;
    my $ret_code = &store_status($storecat_ok{$store}, $retweek);
    if ($ret_code==0)
    {
      #$reject{$store}=1;
      push (@user_message, "Store $storecat_ok{$store}  for week $retweek is set to status $store_status_info - if reprocessing reqd.,please use the SCD3 reprocess option to reset it, and then resubmit\n");
      next LINE;
    }
   }
}


 if($line=~/^P/)
 {
        $P_record="present";

        $NAE=substr($line,1,13);
        $NAE = ("0" x (13 - length ($NAE))) . $NAE if (length ($NAE) < 13);
        #$NAE = &check_digit($NAE) if ($NAE > 9999999);


        $NAE_type=substr($line,14,1);
        $system_code=substr($line,1,2);
        $sales_type=substr($line,15,1);
        $units_type=substr($line,24,1);
        $length_NAE_record=length($line);
        $value_sales=substr($line,16,7);
        $unit_sales=substr($line,25,8);
        $check_digit=substr($NAE,12,1);
        $NAE_without_check_digit = substr($line,1,12);
        $len_NAE_without_cdigit=length($NAE_without_check_digit);

        if($NAE!~/^(\d)+$/)
        {
          if(($len_NAE_without_cdigit !=12)&&($check_digit ne 'X'))
          {
            push(@{$extrastore_line{$retweek}},"$line\n");
            push(@invalid_record,"Line $linecount:  Store $storecat_ok{$store_num} ($store_num): $line \n \t \t \t \tThe NAE is not numeric,length of the NAE without check digit is not 12 and check digit is not X \n");
            if (!defined(%invalid_rec_count))
            {
                $invalid_rec_count{$store_num}=1;
            }
            else
            {
               $invalid_rec_count{$store_num}++;
            }
            goto CHECK;

           }

        }

        if($length_NAE_record !=34)
        {
          push(@{$extrastore_line{$retweek}},"$line\n");
          push(@invalid_record,"Line $linecount:  Store $storecat_ok{$store_num} ($store_num): $line \n \t \t \t \tThe length of NAE record is not as expected (34)\n");
          if (!defined(%invalid_rec_count))
          {
            $invalid_rec_count{$store_num}=1;
          }
          else
          {
            $invalid_rec_count{$store_num}++;
          }
          goto CHECK;
        }


        if($NAE_type!~/[AGKQS]/)
        {
         push(@{$extrastore_line{$retweek}},"$line\n");
         push(@invalid_record,"Line $linecount: Store $storecat_ok{$store_num} ($store_num): $line \n \t \t \t \tInvalid NAE type $NAE_type\n");
         $invalid_rec_cnt++;
         if (!defined(%invalid_rec_count))
         {
           $invalid_rec_count{$store_num}=1;
        }
        else
        {
           $invalid_rec_count{$store_num}++;
        }
         goto CHECK;
        }

        if($NAE_type=~/[GS]/)
        {
         #For any P type records where NAE Type is G or S and the NAE system code is not in the range 21 to 24.
         if ($system_code !~/2[1234]/)
         {
          push(@{$extrastore_line{$retweek}},"$line\n");
          push(@invalid_record,"Line $linecount: Store $storecat_ok{$store_num} ($store_num): $line \n \t \t\t\tStandard NAE has random weight NAE Type\n");
          if (!defined(%invalid_rec_count))
          {
           $invalid_rec_count{$store_num}=1;
          }
          else
          {
            $invalid_rec_count{$store_num}++;
          }
          goto CHECK;
         }
        }

        if($NAE_type=~/K/)
        {
          #Remove leading zeros
          $NAE=~s/^(0)+//;
          $NAE_copy=$NAE;
          $check_digit = chop($NAE_copy);
          if((length($NAE)> 7) || (length($NAE)> 6 && $check_digit eq 'X'))
          {
           push(@{$extrastore_line{$retweek}},"$line\n");
           push(@invalid_record,"Line $linecount: Store $storecat_ok{$store_num}($store_num): $line \n \t \t\t\tStandard NAE has random weight NAE Type\n");
           if (!defined(%invalid_rec_count))
           {
            $invalid_rec_count{$store_num}=1;
           }
           else
           {
            $invalid_rec_count{$store_num}++;
           }
           goto CHECK;
          }
        }

        if($sales_type!~/V/)
        {
         push(@{$extrastore_line{$retweek}},"$line\n");
         push(@invalid_record,"Line $linecount: Store $storecat_ok{$store_num} {$store_num): $line \n \t \t\t\tInvalid Sales Type $sales_type\n");
         if (!defined(%invalid_rec_count))
         {
           $invalid_rec_count{$store_num}=1;
         }
         else
         {
          $invalid_rec_count{$store_num}++;
         }
         goto CHECK;
        }

        if($units_type!~/Q/)
        {
         push(@{$extrastore_line{$retweek}},"$line\n");
         push(@invalid_record,"Line $linecount: Store $storecat_ok{$store_num} {$store_num): $line \n \t \t\t\t  Invalid Unit Type $units_type\n");
        if (!defined(%invalid_rec_count))
        {
         $invalid_rec_count{$store_num}=1;
        }
        else
        {
         $invalid_rec_count{$store_num}++;
        }
        goto CHECK;
        }

       #Checking for invalid characters

       $value_sales=&trim($value_sales);
       if($value_sales !~ /^\d*\.?\d*$/)
       {
         push(@{$extrastore_line{$retweek}},"$line\n");
         push(@invalid_record,"Line $linecount: Store $storecat_ok{$store_num} {$store_num): $line \n \t \t\t\t Value sales not numeric\n");
         if (!defined(%invalid_rec_count))
         {
          $invalid_rec_count{$store_num}=1;
         }
         else
         {
           $invalid_rec_count{$store_num}++;
         }
         goto CHECK;
        }

        $unit_sales=&trim($unit_sales);
        if($unit_sales !~ /^\d*\.?\d*$/)
        {
         push(@{$extrastore_line{$retweek}},"$line\n");
         push(@invalid_record,"Line $linecount: Store $storecat_ok{$store_num} {$store_num): $line \n \t \t\t\t  Unit sales not numeric \n");
         if (!defined(%invalid_rec_count))
         {
           $invalid_rec_count{$store_num}=1;
         }
         else
         {
           $invalid_rec_count{$store_num}++;
         }
         goto CHECK;
        }

 }
 if($line =~/^Q/)
 {
  $NAE_desc=substr($line,1,30);
  if($P_record ne "present")
  {
    push(@{$extrastore_line{$retweek}},"$line\n");
    push(@invalid_record,"Line $linecount: Store $storecat_ok{$store_num}: $line \n \t \t \t \t No NAE for description\n");
    $invalid_rec_cnt++;
  }
  if(!defined $NAE_desc)
  {
   push(@{$extrastore_line{$retweek}},"$line\n");
   if (! defined (%ndf))
   {
    $ndf{$store}=1;
   }
   else
   {
     $ndf{$store}++;
   }
   $NAE_desc="NDF";
   push(@ndf,"Line $linecount : Store $store :  NAE $NAE\n");
  }

 }

 if($line=~/^X/)
 {
     $xfound = "true";
     $store_trailer_store_num=substr($line,15,2);
     if($store_trailer_store_num ne $store_num)
    {
    push(@incomplete_store,"The store number on store trailer does not match the store number on the store header: line $linecount\n");
   push(@incomplete_store,"$line\n");
    }
}


if($line=~/^Y/)
{
 $yfound="true";
 if($xfound eq "false")
 {
   push(@incomplete_store,"Line $linecount: Store $storecat_ok{$store_num}: $line\n \n \t \t No X type record for this Y type record\n");
 }
}

if($line =~/^Z/)
{
   $num_file_trailer++;
   if($xfound eq "false" && $yfound eq "false")
   {
    push(@incomplete_store,"Line $linecount: Store $storecat_ok{$store_num}: $line\n \n \t \t No X and Y type records for this Z record\n");
   }
   elsif($xfound eq "false")
   {
    push(@incomplete_store,"Line $linecount: Store $storecat_ok{$store_num}: $line\n \n \t \t No X type record for this Z type record\n");
    }
    elsif($yfound eq "false")
    {
     push(@incomplete_store,"Line $linecount: Store $storecat_ok{$store_num}: $line\n \n \t \tNo Y type record for this Z type record \n");
    }

}
if($num_file_trailer > 1)
{
 push(@{$extrastore_line{$retweek}},"$line\n");
 push(@invalid_record,"Line $linecount: Store $storecat_ok{$store_num}: $line \n \t \t \t \tUnexpected extra file trailer\n");
 if (!defined(%invalid_rec_count))
 {
    $invalid_rec_count{$store_num}=1;
 }
 else
 {
   $invalid_rec_count{$store_num}++;
 }
 goto CHECK;
}
if($line=~/^[BF]/)
{
   push(@{$extrastore_line{$retweek}},"$line\n");
   push(@invalid_record,"Line $linecount: Store $storecat_ok{$store_num}: $line \n \t \t \t \tUnexpected Record Type\n");
   $invalid_rec_cnt++;
}
 #Check if number of invalid records is greater than fifty
 CHECK:
  $invalid_num_recs=0;

  if(keys %invalid_rec_count > 0 )
  {
    foreach $shop (keys %invalid_rec_count)
    {
     $invalid_num_recs=$invalid_num_recs+$invalid_rec_count{$shop} if(defined(%invalid_rec_count));
    }
  }
  $invalid_num_recs=$invalid_num_recs + $invalid_rec_cnt if (defined($invalid_rec_cnt));

  if ($invalid_num_recs>50)
  {
    push (@gtfifty_message, "\n Error: more than 50 invalid records in $file for $chain_code for week $retweek: process
     for the week terminating.\n\n");
    foreach $store (keys %store_hash)
    {
         my $ret_code = &store_status($storecat_ok{$store}, $retweek);
         system ("$BINPATH/SCD05 $storecat_ok{$store} $week 0 ERF") if($ret_code==1);
    }
    goto REPORT; #to display the report of errors before going to the next week
  }

}  # End of LINE

close(SALES_FILE) or die "cannot close Sales file $basepath/TEMP/$chain/$file\n";
move("$basepath/TEMP/$chain/$file", "$basepath/TEMP/holding/$chain_code$file"); # Move Sales file to holding directory
#}
REPORT: foreach $week (keys %extrastore_line)
        {
           $week=&trim($week);
           open (OUTFILE,">$chain_path/REJECTED/stores.$week.temp")
           or die "Cannot open file $chain_path/REJECTED/stores.$week.temp: $!\n" ;
           print OUTFILE @{$extrastore_line{$week}};
           close OUTFILE or die "Cannot close file $chain_path/REJECTED/stores.$week.temp: $!\n" ;
        }

        foreach $week (keys %wrongweek_line)
        {
          $week=&trim($week);
          open (OUTFILE,">$chain_path/REJECTED/weeks.$week.temp")
          or die "Cannot open file $chain_path/REJECTED/weeks.$week.temp: $!\n";
          print OUTFILE @{$wrongweek_line{$week}};
          close OUTFILE or die "Cannot close file $chain_path/REJECTED/weeks.$week.temp:$!\n";
        }

        #foreach $retweek (sort keys %{$salesfile_hash_ref})
        #{
         push(@user_message,"\nReport for $chain for ret processing week $retweek\n");

        $extra_header = @extra_file_header;
        if($extra_header > 0)
        {
         push(@user_message,@extra_file_header);
        }
        $arr_len = @missing_store_header1;
        if($arr_len > 0)
        {
         push(@user_message,"\nMissing Store header1(E)for the records:\n");
         push(@user_message,@missing_store_header1);
        }
        #to display all the extra stores
        if (keys %extra_store > 0)
        {
         push(@user_message,"\nExtra stores\n");
         foreach my $store (sort keys %extra_store)
         {
          push(@user_message,$store."\n");
         }
        }

        $incomplete_len = @incomplete_store;
        if($incomplete_len > 0)
        {
         push(@user_message,"\nIncomplete Stores:\n");
         push(@user_message,@incomplete_store);
        }
        $incorrect_week = @incorrect_store_week;
        if($incorrect_week > 0)
        {
         push(@user_message,"\nStores for incorrect week\n");
         push(@user_message,@incorrect_store_week);
        }
        $invalid_record_length = @invalid_record;
        if($invalid_record_length > 0)
        {
         push(@user_message,"\nInvalid Lines:\n");
         push(@user_message,@invalid_record);
        }
        $ndf_length = @ndf;
        if($ndf_length > 0)
        {
         push(@user_message, "\n No Description (NDF)  :\n");
         push(@user_message,@ndf);
        }
        undef @ndf;

        #to display greater than 50  error message
        if (defined(@gtfifty_message))
        {
         push(@user_message,"\n");
         push(@user_message,@gtfifty_message);
        }
        undef @gtfifty_message;
        # Process Valid Stores - Write output
        $new_units_to_weight = &process_valid_stores ($filelist_hash_ref,$retweek, $new_units_to_weight, $units_to_weight);
$units_to_weight
 } # WEEK
 &write_units_to_grams ( $units_parm_file, $new_units_to_weight);
}  # End of the subroutine
# Process Valid stores
sub process_valid_stores
{
 my ($salesfile_hash_ref, $week, $new_units_to_weight, $units_to_weight) = @_;
 foreach $store(sort keys %valid_store)
 {
    $rec_cnt = 0;
    $total_sales = 0;
    $total_lines_X = 0;
    $total_sales_X = 0;
    @valid_store=split("~",$valid_store{$store});
    my $cRTN = 1;
    foreach $valid_record(@valid_store)
    {
     if($valid_record =~/^P/)
     {
       $rec_cnt++;
       $NAE = substr($valid_record,1,13);
       $NAE = ("0" x (13 - length ($NAE))) . $NAE if (length ($NAE) < 13);
       $c_digit = substr($valid_record,13,1);
       # If the check digit is X convert it to 0
       if($c_digit =~/X$/ )
       {
        $NAE =~ s/X$/0/;
       }
       $NAE = &check_digit($NAE) if ($NAE > 9999999);
       $NAE_no_cd = substr($NAE, 0, 12);
       $MF_RW_flag="";
       $NAE_type=substr($valid_record,14,1);
       $sales_sign=substr($valid_record,23,1);
       $sales_units=substr($valid_record,25,8);
       $sales_value=substr($valid_record,16,7);
       if($sales_sign =~/-/)
       {
         $sales_value = -($sales_value);
       }
       $total_sales = $total_sales + $sales_value;
       if($NAE=~/^2[1234]/ && $retweek <= 1478)
       {
        $MF_RW_flag="O";
       }
       if($NAE=~/^2[1234]/ && $retweek >= 1479)
       {
         if($NAE_type eq "G")
         {
           $sales_units=int($sales_units/10);
           $MF_RW_flag="K";
           if($sales_units==0)
           {
            # Reject the record
            push(@{$extrastore_line{$retweek}},"$valid_record\n");
           }
         }
         if($NAE_type=~/[KS]/)
         {
           $MF_RW_flag="S";
           if ( $NAE_type eq "S" and  $NAE =~ m/^23/ ) {
               if ( defined $$units_to_weight{$NAE_no_cd} and $$units_to_weight{$NAE_no_cd} ne 1 ) {
                   if ( $$units_to_weight{$NAE_no_cd} != 0 ) {
                       $sales_units *= $$units_to_weight{$NAE_no_cd};
                       if ( $sales_units > 999999 ) {
                           push @user_message, "Units value over 6 length for store $store, NAE $NAE: $sales_units\n";
                           next;
                       }
                       $MF_RW_flag = "K";
                   }
               } else {
                   if ( defined $$units_to_weight{$NAE_no_cd} ) {
                       $$new_units_to_weight{$NAE_no_cd} = [0] ;
                   } else {
                       $$new_units_to_weight{$NAE_no_cd} = [1] ;
                   }
               }
           }
         }
       }
      if($sales_units!=0)
      {
        $price=$sales_value/$sales_units;
      }
      else
      {
        $price=0;
      }
      $price = int($price + .5 * ($price <=> 0));
      $sales_units = int($sales_units + .5 * ($sales_units <=> 0));
      #$sales_value = int($sales_value + .5 * ($sales_value <=> 0));
     }# End of if for P
     if($valid_record =~/^Q/)
     {
        $desc=substr($valid_record,1,30);
        $desc="NDF" if (!defined $desc);
        if ( defined $$new_units_to_weight{$NAE_no_cd} && ! defined $$new_units_to_weight{$NAE_no_cd}[1] ) {
            $$new_units_to_weight{$NAE_no_cd}[1] = $desc;
        }
     }
     if($valid_record=~/^X/)
     {
       $total_line_X = substr($valid_record,17,6);
       $total_sales_X = substr($valid_record,23,9);
     }
     $cRTN++;
     if($no_weight eq "true")
     {
       $out_hash{"$NAE-$store"}=[$storecat_ok{$store},$week,"0",$NAE,$desc,$sales_value,$sales_units,$price,"","","",$MF_RW_flag,""];
     }
     else
     {
       $out_hash{"$NAE-$store"}=[$storecat_ok{$store},$week,"0",$NAE,$desc,$sales_value,$sales_units,$price,"","","","",""];
     }
     # pack the values required for the output file
     $out_line = pack("A6 A4 A A13 A100 A10 A6 A8 A2 A10 A1 A1 A16",$out_hash{"$NAE-$store"}[0],$out_hash{"$NAE-$store"}[1],$out_hash{"$NAE-$store"}[2],$out_hash{"$NAE-$store"}[3],$out_hash{"$NAE-$store"}[4],$out_hash{"$NAE-$store"}[5],$out_hash{"$NAE-$store"}[6],$out_hash{"$NAE-$store"}[7],$out_hash{"$NAE-$store"}[8],$out_hash{"$NAE-$store"}[9],$out_hash{"$NAE-$store"}[10],$out_hash{"$NAE-$store"}[11],$out_hash{"$NAE-$store"}[12]);
    $out_line{"$NAE-$store"} = $out_line;
}  # End of foreach $valid_record(@valid_store)
   #Writing output info into the output file for the given week
   foreach $NAE_store(sort keys %out_line)
   {
     push(@{$out_file{"$week-$store"}},$out_line{$NAE_store});
   }
   if (defined(%out_file))
   {
     foreach $week_store (keys(%out_file))
     {
       @week_store=split(/-/,$week_store);
       $week=$week_store[0];
       $store=$week_store[1];
       $t_store=$storecat_ok{$store};
       open (OUTFILE,">$chain_path/$t_store/INPUT/D$t_store.$week")   or die  "Cannot open file $chain_path/$t_store/INPUT/D$t_store.$week: $!\n" ;
       my $seq=0;
       undef(@seq_lines) if (defined(@seq_lines));
       foreach $outline (@{$out_file{$week_store}}) # for each sales output record
       {
         $seq++;
         $seq = ("0" x (7 - length ($seq))) . $seq if (length ($seq) < 7);
         my ($outline1, $outline2) = unpack ("A11 A167", $outline); # Split output line to 2 parts to insert sequence number.
                # insert the sequence number in to the sales output record.
         my $seq_line = pack ("A11 A7 A167", $outline1, $seq, $outline2); # Insert Sequence number in between  and form the complete output line.
         push (@seq_lines, $seq_line ."\n"); # push the output line to Sequence lines array.
       }
       print OUTFILE @seq_lines; # write the entire lines of the sales output records to the file specified.
       close OUTFILE or die "Cannot close file $chain_path/$t_store/INPUT/D$t_store.$week: $!\n" ;
       push (@output_file, "$chain_path/$t_store/INPUT/D$t_store.$week");
     }
     undef %out_file;
     undef %out_line;
  }
     if($total_line_X != $rec_cnt)
     {
      push(@misbalancing_stores,"$storecat_ok{$store}($store)\t\t\t File Totals :Lines $rec_cnt \n \t\t\t\t\ Store Trailer Totals: Lines $total_line_X\n");
     }
    if($total_sales_X != $total_sales)
    {
     push(@misbalancing_stores,"$storecat_ok{$store}($store)\t\t\t File Totals: Sales $total_sales \n  \t\t\t\t\ Store Trailer Total: Sales $total_sales_X\n");
    }
REPORT1:
     foreach $week (keys %extrastore_line)
     {
      $week=&trim($week);
      open (OUTFILE,">$chain_path/REJECTED/stores.$week.temp") or die "Cannot open file $chain_path/REJECTED/stores.$week.temp: $!\n" ;
      print OUTFILE @{$extrastore_line{$week}};
      close OUTFILE or die "Cannot close file $chain_path/REJECTED/stores.$week.temp: $!\n" ;
     }
      $mis_len = @misbalancing_stores;
      if($mis_len > 0)
      {
        push(@user_message,"Misbalancing Stores\n");
        push(@user_message,@misbalancing_stores);
      }
 }
 undef @misbalancing_stores;
 return $new_units_to_weight;
}  # End of the subroutine
sub write_report{
 open(REPORT,">$chain_path/TEMP/report_rama")or die "Can not open $chain_path/TEMP/report_rama: $!\n";
 print REPORT @user_message;
 close REPORT or die"Can not close $chain_path/TEMP/report_rama: $!\n";
}

code part 2

# Run the main SCD job.                                                        *
sub run_SCD_main
{
  # Requires refs to lists of files
  my ($out_file) = @_;
  my (@date) = Today();
  $date[0] = substr($date[0],2,2);
  $date[1] = "0" . $date[1]   while length($date[1]) < 2;
  $date[2] = "0" . $date[2]   while length($date[2]) < 2;
  $date = join ("",@date);
  # Run SCD02.sh for each SCD temp file
  foreach $filename (@$out_file)
  {
   my $ret_code = system ("$SHELLPATH/SCD02.sh $filename N NL >> /SCD/log/c$date.$chain_code");
  }
}
#*******************************************************************************
#  Initial set up
#*******************************************************************************
# Check correct no of command line parameters
if ((@ARGV ne 2) && (@ARGV ne 1))
{
         printf "Usage: rama.pl <chain code> <ret store number/sub chain number>\n";
         printf "Usage: rama.pl <chain code>\n";
         exit;
}
if (@ARGV == 1)
{
         ($chain_code) = @ARGV;
         $subchain_store = "01";
}
else
{
         ($chain_code, $subchain_store) = @ARGV;
}
# Read chain code and subchain or store from cl
# Split prog name delimited by "\" to remove
# directory. Pop last list element as chain
@chain = split ("/", $0);
$chain = pop ( @chain );
undef (@user_message);
# Check base dirs exist
unless ( -d "$basepath/CHAINS/$chain" || -d "$basepath/TEMP/$chain")
{
  push (@user_message, "$chain not defined to SCD3\n");
  &end_processing();
}
# Set up chain path
$chain_path = "$basepath/CHAINS/$chain";
# Create base output dirs
&create_sub_directories (["$chain_path/DESC", "$chain_path/REJECTED", "$chain_path/TEMP"]);
# Get list of  files to process
($filelist_hash_ref) = &get_file_list($chain);
# If processing a store
if (length($subchain_store) == 6)
{
  # check if the store is valid
  $subchain = &check_store($chain_code,$subchain_store,$filelist_hash_ref);
}
else
{
 # No subchain for holland so set default subchain 01
  $subchain = "01";
}
# Get list of stores for subchain or abend process
open (STOREFILE, "$BINPATH/SCD09all $chain_code|")
          or die "Cannot fork to process SCD09all $!\n";
# Read store file into list
@storecat = <STOREFILE>;
close STOREFILE;
#Open output file
open (STOREFILE, ">$chain_path/TEMP/storecat.dat")
      or die "Cannot open $chain_path/TEMP/storecat.dat $!\n";
# For each line in the list of stores
foreach $store_line (@storecat)
{
  # Write the line to the output file
  print STOREFILE $store_line;
  # Remove trailer newline
  chomp($store_line);
  # Interpret outputline
  ($ret_store, $raw_store) = unpack ("A6 x A10", $store_line);
  # Add to valid store lookup hash
  if (! defined $storecat_ok{$raw_store} && ! defined $storecat_reject{$raw_store})
  {
        $storecat_ok{$raw_store} = $ret_store;
  }
  else
  # If it exists already on either the valid
  # or invalid store lookup hashes, it is an
  # error. Add to invalid store lookup hash
  # and remove from valid store lookup hash
  {
      $storecat_reject{$raw_store} = 1;
      delete $storecat_ok{$raw_store};
  }
  # Create store subdirectories
  &create_sub_directories(["$chain_path/$ret_store", "$chain_path/$ret_store/INPUT"]);
  # End of store cat line processing
}
# Close storecat file
close STOREFILE;
# Get chain info
$retailer_format = &read_table($chain_code, $subchain, $chain);
die "Cannot find volume indicator\n"  if $retailer_format eq "NR";
if ($$retailer_format[16] eq "Y"){
 # Set family to O for sys 21, 22, 23 and 24 so correct processing on mainframe
 $no_weight = "true";
}
else
{
 $no_weight ="";
}
# Store validation - reject invalid records
&store_validation ($filelist_hash_ref,$chain_code,$subchain_store,$chain);
# Writing Report to a temporary storage
&write_report;
# Run the SCD load program
$ret_code = run_SCD_main(\@output_file);
&end_processing;

output

Output :

Stores for incorrect week
Store (0)  Close date 210406 
Store 055078(01)  Close date 210406 
Store 055078(01)  Close date 210406 
Store 055078(01)  Close date 210406 
Store 055078(01)  Close date 210406 
Store 055078(01)  Close date 210406 
Store 055078(01)  Close date 210406 
Store 055078(01)  Close date 210406 
Store 055078(01)  Close date 210406 
Store 055078(01)  Close date 210406 
Store 055078(01)  Close date 210406 
----
Store 055081(05)  Close date 210406 
Store 055081(05)  Close date 210406 
Store 055081(05)  Close date 210406 
Store 055081(05)  Close date 210406 
Store 055081(05)  Close date 210406 
Store 055081(05)  Close date 210406 
Store 055081(05)  Close date 210406 
Store 055081(05)  Close date 210406 
Store 055081(05)  Close date 210406 
Store 059423(06)  Close date 210406 
Store 059423(06)  Close date 210406 
Store 059423(06)  Close date 210406 
Store 059423(06)  Close date 210406 
Store 059423(06)  Close date 210406 
Store 059423(06)  Close date 210406 
type or paste code here

i tried the below code changes. is it right way to do the modification.

 if($closingdayerror eq "true")
 {
    push(@{$wrongweek_line{$iriweek}},"$line\n")unless($line =~/^E/);
	foreach $store(keys %unique)
	{
    push(@incorrect_store_week,"Store $unique{$storecat_ok{$store_num}($store_num)} Close date $close_date \n");	
 }
 }```
use strict;
use warnings;

my @array = qw/yes no maybe true false false perhaps no/;

my %seen;

foreach my $string (@array) {

    next unless $seen{$string}++;
    print "'$string' is duplicated.\n";
}

Linux sysadmin blog - Linux/Unix Howtos and Tutorials - Linux bash shell scripting wiki