Append the nested case statement content with outer case statement's content

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jhulan
    New Member
    • Oct 2016
    • 1

    Append the nested case statement content with outer case statement's content

    Inputfile.txt is given file and output.txt is expected output in attachment.

    In this situation we have to deal with nested case statements, which you can observe by seeing the input file and output given in attachment.
    I got the output for one level nested case statement. first I saved the content between first "case" and last "esac" in a temp file and run the below given code.
    Code:
    open (data,"<input.txt");
    while (<data>) {
    $para1;  $para2;
      unless (/case/../esac/){
          if(/(.*)\)(.*)$/) {
        $para1=$1;
         $var=$2;       
         }
         else  {  $var=$_; }
    print $para1.$var."\n";
    }
       if (/case/../esac/)  {
            if(/(.*)\)(.*)$/) {
           $para2=$1;
           $var=$2;       
           }
          else  {  $var=$_; }        
    print $para1.$para2.$var."\n";
    }   }
    close data;
    I need your help and suggestion to get output for multiple nested case statement.
    Thank you.
    Attached Files
  • RonB
    Recognized Expert Contributor
    • Jun 2009
    • 589

    #2
    First, you're missing 2 very important pragmas/modules which should be in every perl script you write.
    Code:
    use strict;
    use warnings;
    The strict pragma will require you to declare your vars, which is done with the my keyword.

    When needing to extract data in nested blocks, you should be using a module designed for that purpose.

    Text::Balanced - Extract delimited text sequences from strings.

    Comment

    Working...