2015-12-02 152 views
0
## some lines 
## cell (a) { area : 0.898; power: 0.867; 
     ....(some parameters values) 
    } 
    pin(a1) { power: 0.767; (some more parameters specific to pins) 
    timing() { 
     ## again some parameters value.... 
    } 

我的文件包含大約300個這樣的單元,這些單元位於文件之間。我想分析的文件,什麼知道所有變量參數,我想下面的代碼,但它是沒有用的如何從perl文件中提取一些特定的信息?

while (defined($line=<$fh>)) { 
    if ($line =~ /cell \(\w+\) \{/../cell \(\w+\) \{/) { 
     print $result "$line \n"; 
    } 
} 

我想要得到的值在{}也是,但,不知道怎麼弄的我的代碼中括號內有括號。請幫忙。


感謝ü所有的help..I寫了代碼考慮到標屬性(忽略括號內的所有屬性。),但我現在面臨一個很奇怪的問題。我在代碼中遇到了if($ line =〜/ cell(\ w /../ cell(\ w /))的問題。對於第一個文件,它檢測到有單元格的行(字段並從那裏開始,但用於第二文件時,它從第一行自身開始

open $result_file1, ">", "file1.txt"; 
open $result_file2, ">", "file2.txt"; 
open $fl1, $file1; open $fl2, $file2; 

sub file_reader { 
    ($fh, $indx) = @_; 
    $count = 0; 
    undef @temp; undef @pram; 
    while (defined($line=<$fh>)) {  
     if ($line =~ /cell \(\w/../cell \(\w/) { 
     if ($indx == "1") {print $result_file1 "$line\n";} 
     if ($indx == "2") {print $result_file2 "$line\n";} 
     if ($line =~ /cell \(\w/) { 
      @temp = split (' ', $line);} 
     if ($line =~ /\{/) { 
      $count += 1;} 
     if ($line =~ /\}/) { 
      $count = $count - 1; } 
     if (($line =~ /:/) and ($count == 1)) { 
      @pram = split (':', $line);   
      if ($indx == "1") {$file1{$temp[1]}{@pram[1]} = @pram[2];} 
      elsif ($indx == "2") { $file2{$temp[1]}{@pram[1]} = @pram[2];} 
} }} 
close $fh;} 

file_reader($fl1, "1"); 
file_reader($fl2, "2"); 

一塊文件1的輸出的: 細胞(AND2X1){

cell_footprint : "AND2X1 "; 

    area : 7.3728 ; 

    cell_leakage_power : 3.837209e+04; 

    driver_waveform_rise : "preDrv"; 

    driver_waveform_fall : "preDrv"; 

    pg_pin (VDD) { 

     voltage_name : "VDD"; 

     pg_type : "primary_power"; 

    } 

    pg_pin (VSS) { 

     voltage_name : "VSS"; 

     pg_type : "primary_ground"; 

    } 
    ....... 

一塊文件2的輸出的:

/********************************************************************** 

****                **** 

**** The data contained in the file is created for educational **** 

**** and training purposes only and are not recommended   **** 

**** for fabrication            **** 

****                **** 

*********************************************************************** 

****                **** 

爲什麼它無法應用如果條件爲我的第二個文件,那麼範圍?

+6

我們需要一些代碼和一些有代表性的樣本數據才能夠回答。 「類似」不夠好 - 它不需要包含真正的值,但必須具有正確的結構。還有一些說明問題的工作代碼。和一個理想的輸出。沒有這些,這個問題是無法回答的。請參閱[問] – Sobrique

+0

細胞(OR2X4){ \t \t ##一些標屬性,如(前 - cell_footprint: 「OR2X4」) \t \t ##的某些屬性中的屬性 防爆 - (pg_pin(VDD){ \t \t \t voltage_name: 「VDD」; \t \t \t pg_type裏面: 「primary_power」; \t \t \t定時(){##的一些屬性值} \t \t}) 所以,I H我的兩個文件裏有很多這樣的細胞我必須比較兩個文件中存在的特定單元格的所有屬性。 所以,爲此,我想創建一個包含所有參數的數據結構..並且我堅持如何去做.. 請讓我知道如果我必須更加清楚。 –

+0

我已經寫了以前的代碼,首先獲取包含有關特定單元格的細節的行,但是我在行匹配單元格(\ w +){...之下獲得了所有行。這可能是一個小問題,但是作爲即時消息的新的perl,請幫忙。 –

回答

1

我必須猜測您的輸入數據,因此我不太確定問題和目標。
但無論如何,請嘗試更改線路

if ($line =~ /cell \(\w/../cell \(\w/) { 

if ($line =~ /cell \(\w/.. $line =~/cell \(\w/) { 

否則第二正則表達式將針對未初始化的匹配 「$ _」。
我發現了這一點,通過

use strict; 
use warnings; 

這是我最喜歡的工具之一。
順便說一句,你讓我意識到範圍運算符的這種用法,我覺得這很有趣。謝謝。