473,320 Members | 1,870 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

need help for matche and insert

1
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; 
  13.  
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]; 
  17.  
  18. else{ 
  19. print OUTFILE $_; 
  20. $i++; 
  21. close(FILE); 
  22. close(OUTFILE); 
  23.  
The above code did not work. Could you tell me what's wrong this program?
Jul 20 '08 #1
2 1064
KevinADC
4,059 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
nithinpes
410 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
  2.  
Other than this, the rest of the script seems fine and should work.
Jul 21 '08 #3

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

Similar topics

15
by: Jack | last post by:
I have a text file of data in a file (add2db.txt) where the entries are already entered on separate lines in the following form: INSERT INTO `reviews` VALUES("", "Tony's", "Lunch", "Great...
0
by: James Hong | last post by:
Help please, I try to sending an email from my html page using the java applet. but it give error on most of the PC only very few work, what is the error i make the java applet show as below ...
25
by: Bjørn T Johansen | last post by:
I need to write a SQL that calculates the interval between a start time and a stop time. This is the easy part. The problem is that I only have the time part, i.e. no date, so how can I be sure to...
3
by: Steve | last post by:
i pulled an example off the web and modified it somewhat and whats killimg me is that sometimes it works and sometimes it doesnt. the following is the only line that allows this thing to work over...
3
by: rhaazy | last post by:
Using ms sql 2000 I have 2 tables. I have a table which has information regarding a computer scan. Each record in this table has a column called MAC which is the unique ID for each Scan. The...
8
by: Patti | last post by:
I am new to SQL and have created a stored procedure for a .net web application. Unfortunately I had to use a cursor in the stored procedure, so it is taking several minutes to execute because it...
21
by: tizmagik | last post by:
Database consists of the following 4 tables with respective attributes: CUSTOMER(C#, CUSTOMER NAME, ADDRESS), the key is ITEM(I#, ITEM NAME, MANUFACTURER, YEAR), the key is BOUGHT(C#, I#,...
13
by: PinkBishop | last post by:
I am using VS 2005 with a formview control trying to insert a record to my access db. The data is submitted to the main table no problem, but I need to carry the catID to the bridge table...
4
by: CK | last post by:
Good Morning All, Can use use a variable for the FOR clause in a cursor? Example I have DECLARE @a varchar(50), @b varchar(50), @c varchar(50) DECLARE @sql varchar(255) DECLARE @x...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.