how to add/delete the function for two process at the same time in one line?

Viewed 68

i have two process ID QBPP and FPP ine one line in original config file.

L82B:FPP:A:B:C:D:QBPP:D:E:C:F:

for user's input file is

add:L82B:FPP:E:F: #addfunction:DID:processID:binlists
add:L82B:QBPP:A:B:C:
del:L82B:FPP:A:  #delete functions:DID:processID:binlists.
del:L82B:QBPP:E:D:

so based on the user's input file, it has to add bin list at the same time for both FPP and QBPP respective DID. but my code only perform add/delete function only one process for example my out outcome is like L82B:FPP:A:B:C:D:E:F:QBPP:D:E:C:F: the real outcome should come out like : L82B:FPP:B:C:D:E:F:QBPP:A:B:C:F: it doesn't do add/delete function for second process.so i need to run two times to complete action. Kindly help to suggest on it. thanks alot for your help :)

below is my code:

use strict;
use warnings;
#use ReadWrite;
#use Array::Utils qw(:all);
use File::Copy;
use vars qw($requestfile $requestcnt $configfile $config2cnt $my3file $myfile3cnt $new_file $new_filecnt @output);

my $requestfile = "userinput.txt";  
my $configfile = "testbin_file.txt";
my $didfile = "device_test.txt";
my $newdidfile = "device_test.txt.new";
my $backup_file = "testbin_file.txt.backup";
my $new_file = "testbin_file.txt.new";
readFileinString($requestfile, \$requestcnt); 
readFileinString($configfile, \$config2cnt);
readFileinString($didfile, \$myfile3cnt);

copy($configfile, $backup_file) or die "The copy operation failed: $!";
my $total_bin = 60;
open(FILE, '<', $requestfile) or die $!;
while(<FILE>)   
{
    my $newconfigline = "";
    my $oldconfigline = "";
    my $newDID="";
    my $line = $_;
    chomp $line ;

my ($action,$DID,$processID,$testbins) = split /=/, $line;
 my @processlist = ("FPP","QBPP");
        if($config2cnt=~m/^$DID\:([^\n]+)$/m) #configfile
        {
            my $ConfigFullLine = $1;  #Bin list in config
            my  $Testline = $1;
            my  $TFline = $1; 
            my $testfile = $1;

         my @third;
         foreach my $process (@processlist)
         {   
           $Testline =~ s/:$process/^$process/ig;
           @third = split/\^/,$Testline;
         }         
        $oldconfigline = "$DID:$TFline";
         if($processID eq 'FPP')
         { 
           if($Testline =~ 'FPP')
             {          
                my @First_values = split /\:/, $third[0];               
                my @second_values = split /:/, $testbins;               
                 foreach my $sngletter(@second_values) # Each line from user request
                {  
                  if( grep {$_ eq "$sngletter"} @First_values)
                    {
                        print " $DID - $sngletter - Existing bin..\n\n";
                    }
                    else
                    {
                        print "$DID - $sngletter - Not existing bin..\n\n";
                    }
                }
                 if($action eq 'add')
                {                             
                    $third[0] =~ s/$/:$testbins/g;
                    my $add = "$DID:$third[0]$third[1]";
                    $newconfigline=$add;
                    print"New Added Bin Value $add\n\n";
                }
                elsif ( $action eq 'del')
                {   my $del;
                    foreach my $sngletter(@second_values)
                    {  
                        $third[0] =~ s/$sngletter://g;
                        $del = "$DID:$third[0]:$third[1]";
                        $newconfigline=$del;

                    }
                     print "New Deleted Bin Value $del\n\n";                                                          
                }
                elsif ( $action eq 'rpl')
                {
                    $third[0] =~ s/$third[0]/FPP:$testbins/ig;
                    my $replace = "$DID:$third[0]$third[1]";
                    $newconfigline=$replace;
                    print"Replace Bin Value $replace\n\n";                                    
                }
                else{
                    die("ERROR: Unknown action $action");
                }
             }
            else
             {
               print "This $DID doesn't have FPP process\n";
               my $newadd = "$DID:$processID:$testbins$Testline";
               $newconfigline=$newadd;
                print"New Added FPP process value : $newadd\n\n";
             }       
         }
          if($processID eq 'QBPP')
          {
            if($Testline =~ 'QBPP')
             {          
                 my @First_values = split /\:/, $third[1];

                my @second_values = split /:/, $testbins;

                 foreach my $sngletter(@second_values) # Each line from user request
                {  
                  if( grep {$_ eq "$sngletter"} @First_values)
                    {
                        print " $DID - $sngletter - Existing bin..\n\n";
                    }
                    else
                    {
                        print "$DID - $sngletter - Not existing bin..\n\n";
                    }
                }
                if($action eq 'add')
                {   

                    $third[1] =~ s/$/$testbins/g;
                    my $add1 = "$DID:$third[0]:$third[1]";
                    $newconfigline=$add1;
                    print"New Added Bin Value $add1\n\n";
                }
                elsif ( $action eq 'del')
                {   my $del;
                    foreach my $sngletter(@second_values)
                    {  
                        $third[1] =~ s/$sngletter://g;
                        $del = "$DID:$third[0]:$third[1]";
                        $newconfigline=$del;                 
                    }
                    print "New Deleted Bin Value $del\n\n"; 
                }
                elsif ( $action eq 'rpl')
                {
                    $third[1] =~ s/$third[1]/QBPP:$testbins/ig;
                    my $replace = "$DID:$third[0]:$third[1]";
                    $newconfigline=$replace;
                    print"Replace Bin Value $replace\n\n";                                    
                }
                else{
                    die("ERROR: Unknown action $action");
                }
             }
            else
             {
                print "This $DID doesn't have QBPP process\n";
                my $newadd = "$DID:$Testline$processID:$testbins";
                $newconfigline=$newadd;
                print"New Added QBPP process value : $newadd\n\n";   
             }
          }          
         }
        else{
            print "New DID $DID detected\n"; 
            $newDID=$DID;
            $newconfigline="$DID:$processID:$testbins";
        }
    writeFile($newconfigline,$configfile,$newDID,$didfile,$newdidfile,$oldconfigline);
    }   
sub writeFile()
{
    my ($newconfigline,$configfile,$newDID,$didfile,$newdidfile,$oldconfigline) = @_;
        print "Continue to change config file? 1[Yes]  2[No]\n";
    my $option = <STDIN>;

    print "Selected option $option ";
    if($option == 1)
    {
        if ($newconfigline eq ""){
            print "Error: No config file change due to empty new config\n";
        }
        else{
            my $configcontent="";
            #Read config file
            open(CFILE, "<$configfile") or die "\nFailed Reading File: [$configfile]\n\tReason: $!";
            read(CFILE, $configcontent, -s $configfile, 0);
            #Change config content
            if(length($newDID)>0)
            {
                #Add new DID in Bin config
                $configcontent = "$configcontent$newconfigline\n";
                #print "Success: New DID config\n";

                #Add new DID in DID config
                # open source file for reading
                open(SRC,'<',$didfile) or die $!;
                # open destination file for writing
                open(DES,'>',$newdidfile) or die $!;
                while(<SRC>){
                    print $_;
                    #Currently hardcode to add DID for NONQ only.
                    if (index($_, "NONQ") != -1) {
                        chomp($_);
                        $_ =~ s/$_/$_$newDID/g;
                        $newDID = $_;
                        print DES $_;
                    }
                    else{
                        print DES $_;
                    }
                }
                close(SRC);
                close(DES);
                copy($newdidfile, $didfile) or die "The copy operation failed: $!";
                print "Success: Change DID file with newDID=$newDID\n";

            }
            else{
                #Replace bin list in Bin config
                $configcontent =~ s/$oldconfigline/$newconfigline/g;
                #print "Success: Existing DID config\n";
            }
            close(CFILE);

            #Write Bin config
            open(CFILE, ">$configfile") or die "\nFailed Reading File: [$configfile]\n\tReason: $!";
            print CFILE "$configcontent";
            close(CFILE);
            print "Success: Change config file with newconfigline=$newconfigline\n";
        }
    }
    elsif ($option == 2){
        print"Success: No config file change\n";
    }
    else{
        print"Error: Unknown option selected...\n";
    }
}


sub readFileinString
#------------------>
{
    my $File = shift;
    my $string = shift;
    use File::Basename;
    my $filenames = basename($File);

    open(FILE1, "<$File") or die "\nFailed Reading File: [$File]\n\tReason: $!";
    read(FILE1, $$string, -s $File, 0);
    close(FILE1);
}
1 Answers

This code is way to complicated.

  use strict;
  use warnings;

  my %instructions;
  while (<DATA>) {
     my ($todo, $proc, $id, @l) =split(/:/);
     $instructions{$todo}{$proc}{$id} = [grep {/\S/} @l];
  }

  my @result = ('L82B:FPP:A:B:C:D:QBPP:D:E:C:F:');
  foreach my $line (@result) {
     my ($proc) = split (/:/, $line, 1);
     foreach my $id (keys %{$instructions{add}{$proc}}) {
        foreach my $single_letter (@{$instructions{add}{$proc}{$id}}) {
           if ($line =~ /:$id:(.:)*$single_letter:/) {
              # Already in there nothing todo
           } else {
              $line =~ s/:$id:/:$id:$single_letter:/;
           }
        }         
     }
     foreach my $id (keys %{$instructions{del}{$proc}}) {
        foreach my $single_letter (@{$instructions{del}{$proc}{$id}}) {
           $line =~ s/(:$id:(.:)*)$single_letter:/$1/;
        }         
     }
     print "$line\n";
  }

__DATA__
add:L82B:FPP:E:F:
add:L82B:QBPP:A:B:C:
del:L82B:FPP:A:
del:L82B:QBPP:E:D:

executed, creates the result

L82B:FPP:F:E:B:C:D:QBPP:B:A:C:F:

Which would be ok if the order of the instruction (add/del) is irrelevant.

Related