473,387 Members | 1,553 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,387 software developers and data experts.

Substitution in loop

Hi everyone
I'm frsutrated by this problem and would really appreciate a hand. I may not be doing this the best way, so any suggestions are more than welcome...I want to replace certain sections of a string with other sections (substring replacements).

For example, I would like to go from something like this :
Expand|Select|Wrap|Line Numbers
  1. <TAG val="XXX">a hundred</TAG><TAG val="XXX">a thousand</TAG>
to something like this :
Expand|Select|Wrap|Line Numbers
  1. <TAG val="900">nine hundred</TAG><TAG val="1000">a thousand</TAG>
Instead I am getting something like this :
Expand|Select|Wrap|Line Numbers
  1. <TAG val="1000">nine hundred</TAG><TAG val="1000">a thousand</TAG>
The value which is used in the substitution is always the last match. Now, the puzzling thing is, I do this in other parts of my code and it seems to work. Is there a more reliable/safer way to do this, knowing that a global replace will not do and I am reading lines in a file one at a time. Each line has possibly multiple tags of the same type.

To give you an idea, what I'm doing is something like this :
Expand|Select|Wrap|Line Numbers
  1. $text = "<TAG val="XXX">a hundred</TAG><TAG val="XXX">a thousand</TAG>";
  2. while ($text =~ m/val="XXX"/) {
  3.   $text = &replace($text);
  4.   print $text; # or whatever
  5. }
  6.  
  7. sub replace {
  8.   if ($text =~ val="XXX">(.+)</TAG> {
  9.     $val = &convert($1);
  10.     $text =~ s/val="XXX"/val="$val"/;
  11.   }
  12. }
  13.  
&covert converts the value between tags to the desired format (ie from words to numerical format).


THANKS VERY MUCH for any comments/tips!
Oct 23 '07 #1
2 2408
KevinADC
4,059 Expert 2GB
posted on Tek-Tips also
Oct 23 '07 #2
Yes I also posted this on Tek-Tips.
Oct 24 '07 #3

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

Similar topics

1
by: Phil Hancey | last post by:
I am trying to write a small function to display a recordset in tabular form. I want to define a row only once but then display the relevant data for each row but I got "undefined variable" when I...
6
by: Paddy | last post by:
Hi, I got tripped up on the way eval works with respect to modules and so wrote a test. It seems that a function carries around knowledge of the globals() present when it was defined. (The...
1
by: kollareddy | last post by:
Hi all, I am new to xml/xsd world. I want to know the differences between complex type and element being abstract and if both can be declared so, in case of substitution goups.Also can xsi:type...
4
by: Don | last post by:
I think "macro substitution" is the correct term for what I want to do, but, to be sure, here is a description of what I'd like to know is possible: I want to be able to create a create an object...
5
by: Murali | last post by:
In Python, dictionaries can have any hashable value as a string. In particular I can say d = {} d = "Right" d = "Wrong" d = "test" In order to print "test" using % substitution I can say
10
by: Robert | last post by:
I would like to run a report for each month over two years. I am currently using a date range like this. Then manually substitute the error_time bounds for each month and rerun the query. How...
16
by: John Salerno | last post by:
My initial feeling is that concatenation might take longer than substitution, but that it is also easier to read: def p(self, paragraph): self.source += '<p>' + paragraph + '</p>\n\n' vs. ...
7
by: MtiPaulo | last post by:
I know FoxPro has excellent macro substitution but I am trying to find through MS Access. Wondering if MS Access has Macro Substitution? I will give you my basic example. In FoxPro Language...
6
by: hidrkannan | last post by:
In the below code, I have used 5 different variables var1xxx,...var5xxx using 5 statements. But I would like to loop over the aList elements to substitute for 1 to 5 in the variable names and hence...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.