2015-01-13 33 views
1
if (CONDITION_1) { 
    if (CONDITION_A) { 
     a = 1; 
    } 
} 
elsif (CONDITION_2) { 
    if (CONDITION_B) { 
     if (CONDITION_i) { 
      a = 2; 
     } 
     elsif (CONDITION_ii) { 
      a = 3; 
     } 
     endif 
    } 
    endif 
} 
endif 

我想提取出來作爲字符串的條件,並根據自己的水平一樣將它們打印出來:提取IF-ELSIF根據自己的水平條件

CONDITION_1 
CONDITION_1//CONDITION_A 
CONDITION_2 
CONDITION_2//CONDITION_B 
CONDITION_2//CONDITION_B//CONDITION_i 
CONDITION_2//CONDITION_B//CONDITION_ii 

我應該如何在Perl遞歸代碼時,因爲真正的文件可能有超過3個級別的if-else塊?我無法設法使邏輯正確。非常感謝。

---------- ----------編輯

我想通了這背後的邏輯,這是我的工作代碼:

my (%cond, $lvl); 
open my $fh, '<', $file or die; 
while (<$fh>) { 
    if (/if\s\((\w+)\)/) { 
     $lvl++; 
     push @{ $cond{$lvl} }, $1; 
     for my $i (1 .. $lvl) { 
      print "${ $cond{$i} }[-1]"; 
      print "//" if $i != $lvl; 
     } 
     print "\n"; 
    } 
    elsif (/elsif\s\((\w+)\)/) { 
     push @{ $cond{$lvl} }, $1; 
     for my $i (1 .. $lvl) { 
      print "${ $cond{$i} }[-1]"; 
      print "//" if $i != $lvl; 
     } 
     print "\n"; 
    } 
    elsif (/else/) { 
     push @{ $cond{$lvl} }, 'else'; 
     for my $i (1 .. $lvl) { 
      print "${ $cond{$i} }[-1]"; 
      print "//" if $i != $lvl; 
     } 
     print "\n"; 
    } 
    elsif (/endif/) { 
     $lvl--; 
    } 
} 
close $fh; 
+0

什麼是「東西」?爲了可靠地做到這一點,你需要能夠解析一切,而不僅僅是你想要的東西。 – ysth

+0

它們只是定義的聲明,如'define CONSTANT 3'。然而,if塊的內容並不重要,因爲我只需要提取條件。對於那個很抱歉。 –

+0

是否始終絕對一致的縮進呢?並始終是空格,而不是標籤? – ysth

回答

0

這是完全沒有經過測試的代碼,但是我會使用你的「if-elsif-endif」樣本構造來解決這種情況。我認爲概念上是正確的,但需要一些開發和調試時間:

#!/usr/bin/perl 

use warnings; 
use strict; 

my $in = "if-elsif-endif_feed"; 
my $out = "if-elsif-endif_parsed"; 

my $level; 
my $condition; 
my @levels; 
my @conditions; 

open IN, "<", $in or die "IN: $!\n"; 
open OUT, ">", $out or die "OUT: $!\n"; 

while (<IN>) { 
    if ($_ =~ m/^[elsif].*$/i) { 
     if ($_ =~ m/endif/i) { 
      next; 
     } else { 
      $level = 1; 
      ($condition) = $_ =~ m/[elsif].*\((.*)\).*$/; 
      push @levels, $level; 
      push @conditions, $condition; 
     } 
    } elsif ($_ =~ m/^\s+[elsif].*$/i) { 
     if ($_ =~ m/endif/i) { 
      next; 
     } else { 
      $level++; 
      ($condition) = m/\s+[elsif].*\((.*)\).*$/; 
      push @levels, $level; 
      push @conditions, $condition; 
     } 
    } elsif ($_ =~ m/\s+\}.*$/) { 
     $condition = "end"; 
     push @levels, $level; 
     push @conditions, $condition; 
     $level--; 
    } elsif ($_ =~ m/\}.*$/) { 
     $condition = "end"; 
     $level = 1; 
     push @levels, $level; 
     push @conditions, $condition; 
    } else { 
     next; 
    } 
} 

my $numlevels = @levels; 
my $i; 
my $curlevel; 
my $curcond; 
my $string; 
my $stringc; 

for ($i = 0; $i <= $numlevels; $i++) { 
    $curlevel = $levels[$i]; 
    $curcond = $conditions[$i]; 


    if ($curlevel == 1) { 
     if ($curcond ne "end") { 
      $string = "$curcond"; 
      print "$string\n"; 
      print OUT "$string\n"; 
     } else { 
      $string = undef; 
     } 
    } elsif ($curlevel > 1) { 
     if ($curcond ne "end") { 
      $stringc = $string; 
      if (($levels[++$i] == $curlevel) && ($conditions[++$i] eq "end")) { 
       $string .= "\/\/$curcond"; 
       print "$string\n"; 
      } elsif (($levels[++$i] == $curlevel) && ($conditions[++$i] ne "end")) { 
       $string = $stringc; 
      } 
     } elsif ($curcond eq "end") { 
      print "$string\n"; 
      print OUT "$string\n"; 
     } 
    } 
} 

所以在第一部分中,你基本上是通過文件循環和拍攝水平和狀態信息到陣列,級別爲獨立空間的#。他們將數組解析爲合適的字符串並將其打印在適當的位置。

希望幫助!

+0

爲什麼投下來?我遠嗎?至少留下一些反饋,如果你覺得需要投我的答案... – TheJester1977

+0

對不起,這是真的不是我低估了你。我已經提高了它。感謝您的答覆,我採取了一些想法作爲參考=) –

+0

謝謝你,coff_ee,我很感激。我希望它至少有所幫助! :) – TheJester1977