need help for matche and insert

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • xiuli
    New Member
    • Jul 2008
    • 1

    need help for matche and insert

    I am trying to match two patterns in a file and insert a line. If the patterns matche then insert a line after the second matching pattern line.

    for example,
    I have the following content in a file:

    //This is my source file

    Code:
    pin(name1) { 
    clock; 
    capacitance; 
    direction: input; 
    //this is where i want to insert a line 
    min_pulse; 
    } 
    pin(name2) { 
    clock; 
    capacitance; 
    direction: input; 
    //this is where i want to insert a line 
    max_pulse; 
    }
    I have to match for two patterns "pin" and "direction:inpu t" and then insert a line after the second pattern match.
    Code:
    #!/usr/bin/perl -w 
    open(FILE, " <sdv.lib") ¦ ¦die "can not open this file\n"; 
    open(OUTFILE,">out_sdv.lib") ¦ ¦die "can not create outfie"; 
    $i=0; 
    $search="pin(.+){"; 
    $second="direction(.+)input(.+)"; 
    $add="max_tran: 500;\n"; 
    @source= <FILE>; 
    while(@source){ 
    if($source[$i]=~/$search/){ 
    $j=$i; 
    for($i;$i <=$j+3;$i++){ 
    if($source[$i]=~/$second/){ 
    print OUTFILE $source[$i]." ".$add; 
    } 
    else{ 
    print OUTFILE $source[$i]; 
    } 
    } 
    } 
    
    else{ 
    print OUTFILE $_; 
    } 
    $i++; 
    } 
    close(FILE); 
    close(OUTFILE);
    The above code did not work. Could you tell me what's wrong this program?
    Last edited by numberwhun; Jul 20 '08, 02:47 AM. Reason: Please use code tags
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    Your code, while it could be written better, appears as though it should work or is close to working. I think if you put a little more effort into debugging you will get it working. If not, let me know and I will try and help.

    Comment

    • nithinpes
      Recognized Expert Contributor
      • Dec 2007
      • 410

      #3
      The while(@source) {} loop will result in an infinite loop unless you are removing elements of @source inside the while loop using pop() or shift().
      In your case, since you have used variable index throughout the script, you can replace while(@source) with:
      Code:
      for($i=0; $i<=$#source; $i++) ## remove additional $i++; at the end of your loop
      Other than this, the rest of the script seems fine and should work.
      Last edited by nithinpes; Jul 21 '08, 09:11 AM. Reason: code tags

      Comment

      Working...