467,926 Members | 1,873 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,926 developers. It's quick & easy.

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

Expand|Select|Wrap|Line Numbers
  1. pin(name1) { 
  2. clock; 
  3. capacitance; 
  4. direction: input; 
  5. //this is where i want to insert a line 
  6. min_pulse; 
  7. pin(name2) { 
  8. clock; 
  9. capacitance; 
  10. direction: input; 
  11. //this is where i want to insert a line 
  12. max_pulse; 
I have to match for two patterns "pin" and "direction:input" and then insert a line after the second pattern match.
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w 
  2. open(FILE, " <sdv.lib") ¦ ¦die "can not open this file\n"; 
  3. open(OUTFILE,">out_sdv.lib") ¦ ¦die "can not create outfie"; 
  4. $i=0; 
  5. $search="pin(.+){"; 
  6. $second="direction(.+)input(.+)"; 
  7. $add="max_tran: 500;\n"; 
  8. @source= <FILE>; 
  9. while(@source){ 
  10. if($source[$i]=~/$search/){ 
  11. $j=$i; 
  12. for($i;$i <=$j+3;$i++){ 
  13. if($source[$i]=~/$second/){ 
  14. print OUTFILE $source[$i]." ".$add; 
  15. else{ 
  16. print OUTFILE $source[$i]; 
  18. else{ 
  19. print OUTFILE $_; 
  20. $i++; 
  21. close(FILE); 
  22. close(OUTFILE); 
The above code did not work. Could you tell me what's wrong this program?
Jul 20 '08 #1
  • viewed: 965
2 Replies
Expert 2GB
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.
Jul 20 '08 #2
Expert 256MB
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:
Expand|Select|Wrap|Line Numbers
  1. 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.
Jul 21 '08 #3

Post your reply

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

Similar topics

reply views Thread by James Hong | last post: by
25 posts views Thread by Bjørn T Johansen | last post: by
8 posts views Thread by Patti | last post: by
21 posts views Thread by tizmagik | last post: by
13 posts views Thread by PinkBishop | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.