By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,742 Members | 1,224 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,742 IT Pros & Developers. It's quick & easy.

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

P: 1
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.
Expand|Select|Wrap|Line Numbers
  1. open (data,"<input.txt");
  2. while (<data>) {
  3. $para1;  $para2;
  4.   unless (/case/../esac/){
  5.       if(/(.*)\)(.*)$/) {
  6.     $para1=$1;
  7.      $var=$2;       
  8.      }
  9.      else  {  $var=$_; }
  10. print $para1.$var."\n";
  11. }
  12.    if (/case/../esac/)  {
  13.         if(/(.*)\)(.*)$/) {
  14.        $para2=$1;
  15.        $var=$2;       
  16.        }
  17.       else  {  $var=$_; }        
  18. print $para1.$para2.$var."\n";
  19. }   }
  20. close data;
I need your help and suggestion to get output for multiple nested case statement.
Thank you.
Attached Files
File Type: txt inputfile.txt (678 Bytes, 191 views)
File Type: txt output.txt (232 Bytes, 163 views)
Oct 20 '16 #1
Share this Question
Share on Google+
1 Reply

Expert Mod 100+
P: 589
First, you're missing 2 very important pragmas/modules which should be in every perl script you write.
Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. 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.
Oct 20 '16 #2

Post your reply

Sign in to post your reply or Sign up for a free account.