473,487 Members | 2,467 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Modification between the lines

1 New Member
Hello Perl Experts,

I have a file which looks something like this...

********
"OUT_BUS[9]" + "Q0" + "Q1" + "Q2" + "Q3" + "Q4" + "SO[0]" + "SO[1]" +
"SO[2]" + "SO[3]" + "SO[4]" + "SO[5]" + "SO[6]" + "SO[7]"';
}
ScanStructures {
ScanChain "1" {
ScanLength 22;
ScanEnable "te";
ScanMasterClock "tClock_1" "tClock_2";
}
ScanChain "2" {
ScanLength 22;
ScanEnable "te";
ScanMasterClock "tClock_1";
}
}
************************

Now what I want to do is in this step

1) Find the word ScanStructures in the file (i am able to do this with m/ScanStructures/g)
2) After this word is found in the file, I want to replace word between "" with "Hardik_" eg.
ScanChain "1" {
with
ScanChain "Hardik_1" {
for all the appearance
3) I want to stop this word finding / replacement when } (corresponding to ScanStructures { ) is comming back
4) Please note that this word can apear at any place (beginning / end / middle) in line and at any line.

Can somebody help ??
May 12 '11 #1
2 1422
hsriat
1,654 Recognized Expert Top Contributor
Expand|Select|Wrap|Line Numbers
  1. my $counter = 0;
  2. my @positions;
  3. while ($file =~ /(ScanStructures|\{|\}|\"[^\"]*\")/g) {
  4.   if ($1 eq '{') {
  5.     $counter++;
  6.   }
  7.   elsif ($1 eq '}') {
  8.     $counter--;
  9.   }
  10.   elsif ($1 eq 'ScanStructures') {
  11.     $counter = 0;
  12.   }
  13.   elsif (!index $1, '"') {
  14. #   push @positions, $-[1] if $counter == 1; # if nested ones not required
  15.     push @positions, $-[1] if $counter > 0;
  16.   }
  17. }
  18.  
Now you will have all the positions where you need to insert your string (begin the insertion from EOF moving towards the start).

Note:
If 'ScanStructures' is nested in another 'ScanStructures', it won't work. You will need an array (filo) of counters.
May 12 '11 #2
miller
1,089 Recognized Expert Top Contributor
You don't say what version of perl you're using, but if it's 5.10 or later, I'd suggest using the (?PARNO) feature to capture balanced braces. You also don't say if you want the nested strings to be translated too, but assuming so:

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3.  
  4. my $data = do {local $/; <DATA>};
  5.  
  6. $data =~ s{(ScanStructures (\{(?:(?>[^{}]+)|(?-1))+\}))}{
  7.     my $str = $1;
  8.     $str =~ s/"(.*?)"/"Hardik_$1"/g;
  9.     $str
  10. }eg;
  11.  
  12. print $data;
  13.  
  14. __DATA__
  15. "OUT_BUS[9]" + "Q0" + "Q1" + "Q2" + "Q3" + "Q4" + "SO[0]" + "SO[1]" + 
  16. "SO[2]" + "SO[3]" + "SO[4]" + "SO[5]" + "SO[6]" + "SO[7]"';
  17. }
  18. ScanStructures {
  19.   ScanChain "1" {
  20.     ScanLength 22;
  21.     ScanEnable "te";
  22.     ScanMasterClock "tClock_1" "tClock_2";
  23.   }
  24.   ScanChain "2" {
  25.     ScanLength 22;
  26.     ScanEnable "te";
  27.     ScanMasterClock "tClock_1";
  28.   }
  29. }
  30. foo {
  31.   ScanChain "1" {
  32.     ScanLength 22;
  33.     ScanEnable "te";
  34.     ScanMasterClock "tClock_1" "tClock_2";
  35.   }
  36.   ScanChain "2" {
  37.     ScanLength 22;
  38.     ScanEnable "te";
  39.     ScanMasterClock "tClock_1";
  40.   }
  41. }
  42.  
- Miller
May 12 '11 #3

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

Similar topics

17
4173
by: Douglas Alan | last post by:
Is there a canonical way of iterating over the lines of a file that are null-separated rather than newline-separated? Sure, I can implement my own iterator using read() and split(), etc., but...
10
1839
by: malv | last post by:
I am involved in a major scientific algorithm design problem in which simulation of the underlying physics and visualization play an important role. Algorithm adaptation from run to run often...
1
1569
by: BigAbility | last post by:
regKey = regKey.OpenSubKey("...."); regKey.SetValue("...", "value"); this code doesn't work registry modification not allow in .Net?? how should i do?
9
2925
by: Jay Kim | last post by:
Hi, We're implementing a Windows application using Visual Basic .NET. One of the key features we need to implement is that we should be able to get the accurate byte offset of user selected...
8
1800
by: Ken Capriell | last post by:
I know that subject probably did not adequately explain anything so here goes... I have an access file that needs to be transposed from its current format to a new format so that I can then...
13
1789
by: ts-dev | last post by:
Is it possible to prevent modification of a python file once its been deployed? File permissions of the OS could be used..but that doesn't seem very secure. The root of my question is verifying...
3
1811
by: Tinku | last post by:
Dear C++'ers I like to know how we can modify the contents in a file ex: There is a file.txt with the following data Weather=Cold WhatIDo=SleepOntheBed
7
2608
by: Vlad Dogaru | last post by:
Hello, I suspect this comes up quite often, but I haven't found an exact solution in the FAQ. I have to read and parse a file with arbitrarily long lines and have come up with the following...
9
2124
by: richee | last post by:
Hi all, I am new to Perl but see that it offers some fantastic opportunities and am trying to use it more in problem solving but could do with a little help on a problem thats driving me nuts.......
2
1902
by: Unpopular | last post by:
void directory::modification()//??????????? { clrscr(); cout<< "\n\t @@@@@@ @@@@@ @@@@@ @@@@@@ @@@@@ @ @ @@@@@@ "; cout<< "\n\t=====@ @ @ @ @ @ @@...
0
6967
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7132
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6846
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7341
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5439
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4564
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3076
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
600
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
266
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.