473,471 Members | 2,064 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

string loop

19 New Member
Hello everyone,

I have a bit of code that I want to repeat for a string. I keep getting infinite while loops when I try it though. Any thoughts on where I am going wrong?

Thanks,
Mark

original code:
Expand|Select|Wrap|Line Numbers
  1. tvshow = {}
  2. start = data.find( '>')
  3. end = data.find( '\n', start+1)
  4. firstname = data[start:end]
  5. start2 = data.find ( '\n', start)
  6. end2 = data.find( '\n', start2 + 1 )
  7. start2 = start2 + 1
  8. lastname = data[start2:end2]
  9. tvshow[firstname] = lastname
  10.  
loop code:
Expand|Select|Wrap|Line Numbers
  1. tvshow = {}
  2.     start = 0
  3.     a = 0
  4.  
  5.     while (data):
  6.         if start != -1:
  7.  
  8.             start = data.find( '>')
  9.             end = data.find( '\n', start+1)
  10.             firstname = data[start:end]
  11.             start2 = data.find ( '\n', start)
  12.             end2 = data.find( '\n', start2 + 1 )
  13.             start2 = start2 + 1
  14.             lastname = data[start2:end2]
  15.             tvshow[firstname] = lastname
  16.             start += 1
  17.             end += 1
  18.             start2 += 1
  19.             end2 += 1
  20.         else:
  21.             break
  22.  
Aug 3 '07 #1
5 1832
ghostdog74
511 Recognized Expert Contributor
Hello everyone,

I have a bit of code that I want to repeat for a string. I keep getting infinite while loops when I try it though. Any thoughts on where I am going wrong?

Thanks,
Mark

original code:
Expand|Select|Wrap|Line Numbers
  1. tvshow = {}
  2. start = data.find( '>')
  3. end = data.find( '\n', start+1)
  4. firstname = data[start:end]
  5. start2 = data.find ( '\n', start)
  6. end2 = data.find( '\n', start2 + 1 )
  7. start2 = start2 + 1
  8. lastname = data[start2:end2]
  9. tvshow[firstname] = lastname
  10.  
loop code:
Expand|Select|Wrap|Line Numbers
  1. tvshow = {}
  2.     start = 0
  3.     a = 0
  4.  
  5.     while (data):
  6.         if start != -1:
  7.  
  8.             start = data.find( '>')
  9.             end = data.find( '\n', start+1)
  10.             firstname = data[start:end]
  11.             start2 = data.find ( '\n', start)
  12.             end2 = data.find( '\n', start2 + 1 )
  13.             start2 = start2 + 1
  14.             lastname = data[start2:end2]
  15.             tvshow[firstname] = lastname
  16.             start += 1
  17.             end += 1
  18.             start2 += 1
  19.             end2 += 1
  20.         else:
  21.             break
  22.  
how about correcting your indentation first
Aug 3 '07 #2
bvdet
2,851 Recognized Expert Moderator Specialist
Hello everyone,

I have a bit of code that I want to repeat for a string. I keep getting infinite while loops when I try it though. Any thoughts on where I am going wrong?

Thanks,
Mark

original code:
Expand|Select|Wrap|Line Numbers
  1. tvshow = {}
  2. start = data.find( '>')
  3. end = data.find( '\n', start+1)
  4. firstname = data[start:end]
  5. start2 = data.find ( '\n', start)
  6. end2 = data.find( '\n', start2 + 1 )
  7. start2 = start2 + 1
  8. lastname = data[start2:end2]
  9. tvshow[firstname] = lastname
  10.  
loop code:
Expand|Select|Wrap|Line Numbers
  1. tvshow = {}
  2.     start = 0
  3.     a = 0
  4.  
  5.     while (data):
  6.         if start != -1:
  7.  
  8.             start = data.find( '>')
  9.             end = data.find( '\n', start+1)
  10.             firstname = data[start:end]
  11.             start2 = data.find ( '\n', start)
  12.             end2 = data.find( '\n', start2 + 1 )
  13.             start2 = start2 + 1
  14.             lastname = data[start2:end2]
  15.             tvshow[firstname] = lastname
  16.             start += 1
  17.             end += 1
  18.             start2 += 1
  19.             end2 += 1
  20.         else:
  21.             break
  22.  
Can you show us a sample of the string? In your code, there seems to be no way for the variable 'start' to ever be -1, so the loop would never end. You could break the string up by splitting on '\n' and extract the data by iterating on the list.
Aug 3 '07 #3
moconno5
19 New Member
Thanks for the replies.
My thinking was that when there is no data left in the string I am reading (which I read in from a file), then start would equal -1.

The sample data I am working with is:

>Musmusculuslet-7g
UGAGGUAGUAGUUUGUACAGU
>Musmusculuslet-7i
UGAGGUAGUAGUUUGUGCUGU
>MusmusculusmiR-1
UGGAAUGUAAAGAAGUAUGUA

I also practiced by using this:

>Jerry
Seinfeld
>Cosmo
Kramer

what is the problem with the indentation?

Mark
Aug 3 '07 #4
bvdet
2,851 Recognized Expert Moderator Specialist
I would think a solution like this would be better:
Expand|Select|Wrap|Line Numbers
  1. s = ">Musmusculuslet-7g\nUGAGGUAGUAGUUUGUACAGU\n>Musmusculuslet-7i\nUGAGGUAGUAGUUUGUGCUGU\n>MusmusculusmiR-1\nUGGAAUGUAAAGAAGUAUGUA"
  2.  
  3. dd = {}
  4. sList = s.split()
  5. while len(sList) > 0:
  6.     dd[sList.pop(0)] = sList.pop(1)
  7.  
  8. print dd
Output:
>>> {'>Musmusculuslet-7i': 'UGAGGUAGUAGUUUGUGCUGU', '>MusmusculusmiR-1': 'UGGAAUGUAAAGAAGUAUGUA', '>Musmusculuslet-7g': 'UGAGGUAGUAGUUUGUACAGU'}
>>>
Aug 3 '07 #5
moconno5
19 New Member
Thanks! That has done the trick

Mark
Aug 3 '07 #6

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

Similar topics

12
by: Kamilche | last post by:
I was looking for a way to speed up detecting invalid characters in my TCP string, and thought of yet another use for the translate function! If you were to 'translate out' the bad characters, and...
9
by: No Such Luck | last post by:
I have a function which requires me to loop from the end of a string to the beginning on a char by char basis: int foo (char string) { unsigned int i; for(i = strlen(string); i >= 0; i--) {...
19
by: Paul | last post by:
hi, there, for example, char *mystr="##this is##a examp#le"; I want to replace all the "##" in mystr with "****". How can I do this? I checked all the string functions in C, but did not...
17
by: Chad Myers | last post by:
I've been perf testing an application of mine and I've noticed that there are a lot (and I mean A LOT -- megabytes and megabytes of 'em) System.String instances being created. I've done some...
35
by: Cor | last post by:
Hallo, I have promised Jay B yesterday to do some tests. The subject was a string evaluation that Jon had send in. Jay B was in doubt what was better because there was a discussion in the C#...
7
by: Brian Mitchell | last post by:
Is there an easy way to pull a date/time stamp from a string? The DateTime stamp is located in different parts of each string and the DateTime stamp could be in different formats (mm/dd/yy or...
7
by: bcpkh | last post by:
Hello All I need to check a string to make sure it does not contain any non numeric characters, the problem that I face is that the string is fairly long, 2784601121574585949, strtol etc. can't...
1
by: kellysgirl | last post by:
Now what you are going to see posted here is both the set of instructions I was given..and the code I have written. The instructions I was given are as follows In this case, you will create...
4
by: chellemybelle | last post by:
Hello, I basically have made a little cheezy slideshow and would like to add captions to each pic as it loops through. I've tried everything I can think of. I'm assuming I might need to use an...
3
by: yogi_bear_79 | last post by:
I'm sure I have a few things wrong here. But I am stuck on how to do a recurring search. Also my statement cin >quote; acts weird. If I enter more than one word it blows right past cin >findMe;...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
1
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
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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
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...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.