0

I was working on a script that needs to produce foo for the first two lines and bar for the last three. There are two issues i am running into here.

  1. How do I get Perl to ignore the double quotes around the first foo?

  2. How do I get it recognize the backslash as a continuation line? -

Sample input:

reset -name "foo"
quasi_static -name foo
reset \
-name bar
set_case_analysis -name "bar"

My Code:

   if (/^\s*set_case_analysis.*\-name\s+(\S+)/) 
   {
      $set_case_analysis{$1}=1;
      print "Set Case $1\n";
   } 
   elsif (/^\s*quasi_static.*\-name\s+(\S+)/) 
   {
      $quasi_static{$1}=1;
      print "Quasi Static $1\n";
   } 
   elsif (/^\s*reset\s+.*\-name\s+(\S+)/) 
   {
      $reset{$1}=1;
      print "Reset $1\n";
    }
synarchy007
  • 19
  • 1
  • 4
  • Are you reading this data in line-by-line from a file? Does the line continuation character always appear in the same place? – i alarmed alien Aug 07 '18 at 19:38
  • Yes, the data is being read line-by-line. The line continuation character does not always appear in the same column of text but when it appears, it is on the last space of the line. @ialarmedalien – synarchy007 Aug 07 '18 at 20:09

1 Answers1

0

If you're going through a file line by line, you can keep partial lines in a variable and concatenate them with the next line. Read through the code - I have commented the functionality.

my $curr;
my $txt;
open ( IN, "<", 'yourinputfile.txt' ) or die 'Could not open file: ' . $!;
while (<IN>) {
  chomp;
  # if the line ends with a backslash, save the segment in $curr and go on to the next line
  if ( m!(.*?) \$! ) {
    $curr = $1;
    next;
  }
  # if $curr exists, add this line on to it
  if ( $curr ) {
    $curr .= $_;
  }
  # otherwise, set $curr to the line contents
  else {
    $curr = $_;
  }

  if ( $curr =~ /set_case_analysis -name\s+\"?(\S+)/) {
      # if the string is in quotes, the regex will leave the final " on the string
      # remove it
      ( $txt = $1 ) =~ s/"$//;
      print "Set Case $txt\n";
      $set_case_analysis{$txt}=1;
   }
   elsif ($curr =~ /quasi_static -name\s+(\S+)/) {
      ( $txt = $1 ) =~ s/"$//;
      print "Quasi Static $txt\n";
      $quasi_static{$txt}=1;

   }
   elsif ($curr =~ /reset .*?-name\s+\"?(\S+)/) {
      ( $txt = $1 ) =~ s/"$//;
      print "Reset $txt\n";
      $reset{$txt}=1;
  }
  # reset $curr
  $curr = '';
}

You could make it more compact and neat by doing something like this:

if ( $curr =~ /(\w+) -name \"?(\S+)/) {
      ( $txt = $2 ) =~ s/"$//;
      $data{$1}{$txt}=1;
}

You would get a nested hash structure with the three keys, set_case_analysis, quasi_static, and reset, and the various different values from -name.

%data = (
  quasi_static => ( foo => 1, bar => 1 ),
  reset => ( pip => 1, pap => 1, pop => 1 ),
  set_case_analysis => ( foo => 1, bar => 1 )
);
i alarmed alien
  • 9,412
  • 3
  • 27
  • 40
  • I'm pretty new to perl so I'm a bit lost with the regular expressions, however I am able to follow along.I keep getting errors that say 'requires specific package name'. Are there any packages that I need to import? – synarchy007 Aug 08 '18 at 22:11
  • all the above is vanilla perl that should work without any special settings. Is there more code that might be causing the error? Can you post the full error message? – i alarmed alien Aug 09 '18 at 04:20
  • I am currently getting syntax errors around the brackets of my first section of code, posted above. Should I remove it since all it is doing is pushing values to variables? Is that not what your edits do? – synarchy007 Aug 09 '18 at 21:28