473,666 Members | 2,449 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to get a while loop to recognize an extracted number

14 New Member
I am writing a fairly simple script that is supposed to print out lines from a txt file and group them based on where I want them. The general format of the files I am using the script on is as follows:

662376 [S] |GCGG | | |
662375 [S] |CGCC | | |
662374 [S] |GCGG | | |
662373 [H] |CATC | | |
662371 [Y] |TCCC | | |
662369 [M] |CACCC| | |
662367 [Y] |TCTTT| | |
662365 [S] |GCGGG| | |
662364 [R] |AGAAA| | |
662360 [M] |CACCC| | |
662359 [W] |ATAAA| | |
662358 [S] |CGCCC| | |
662357 [M] |ACAAA| | |
662356 [R] |GAAAG| | |
662354 [S] |GCGGG| | |
662353 [R] |AGAAA| | |
662352 [R] |GAGGG| | |
662350 [K] |TGTTT| | |
662349 [Y] |CTCCC| | |
662348 [M] |ACAAA| | |
662347 [R] |GAGGG| | |
662376 [S] |GCGG | | |
662375 [S] |CGCC | | |
662374 [S] |GCGG | | |
662373 [H] |CATC | | |
662371 [Y] |TCCC | | |
662369 [M] |CACCC| | |
662367 [Y] |TCTTT| | |
662365 [S] |GCGGG| | |
662364 [R] |AGAAA| | |
662360 [M] |CACCC| | |
662359 [W] |ATAAA| | |
662358 [S] |CGCCC| | |
662357 [M] |ACAAA| | |
662356 [R] |GAAAG| | |
662354 [S] |GCGGG| | |
662353 [R] |AGAAA| | |
662352 [R] |GAGGG| | |
662350 [K] |TGTTT| | |
662349 [Y] |CTCCC| | |
662348 [M] |ACAAA| | |
662347 [R] |GAGGG| | |


and here is the code:

Expand|Select|Wrap|Line Numbers
  1. print "What is the start of the ORF?\n";
  2. $orf_beg = <STDIN>;
  3.  
  4. open (IN, '/home/asession/pracperl/bin/SNPYNR+-1kb.txt') || die "nope\n";
  5.  
  6. while (<IN>) {
  7.  
  8.     chomp;
  9.  
  10.     $position = substr $_, 1, 11;               # extract the position
  11.     $type_of_polymorphism = substr $_, 13, 3;   # extract the polymorphism type, same as ref if no polymorphism
  12.     $ref_seq = substr $_, 17, 1;                # extract the cerevisiae ref sequence
  13.     $para_seq = substr $_, 18, 1;               # extract the paradoxus sequence
  14.     $mika_seq = substr $_, 19, 1;               # extract the mika sequence
  15.     $kudr_seq = substr $_, 20, 1;               # extract the kudr sequence
  16.     $baya_seq = substr $_, 21, 1;               # extract the bayanus sequence
  17.  
  18.  
  19.     $baya_seq =~ s/\s+/x/g;
  20.     #$position =~ tr/ //d;
  21.  
  22.     foreach ($position) {
  23.         while ($postion > $orf_beg) {
  24.         print "$position $ref_seq $para_seq $baya_seq\n";
  25. }}
  26.  
  27. }
  28. close (IN);
  29.  
  30.     print "\n";
  31.  
Basically the problem I am having is that the while loop withing the foreach loop is not recognizing the position variable as a number, I tried to remove the white space, thinking that was an issue, but it did not work. Any suggestions?
Jul 17 '08 #1
16 2301
KevinADC
4,059 Recognized Expert Specialist
If this is supposed to be input to the perl program, remove the newline from the end:

Expand|Select|Wrap|Line Numbers
  1. print "What is the start of the ORF?\n";

should be:

Expand|Select|Wrap|Line Numbers
  1. print "Enter the start of the ORF: ";
  2. chomp($orf_beg = <STDIN>);
Jul 17 '08 #2
sessmurda
14 New Member
Thats not the variable that perl isnt recognizing, the $position is the problem, I defined orf beg before opening the file handle so that during the loop it wouldnt ask me to input the start of the ORF before every iteration.
Jul 17 '08 #3
eWish
971 Recognized Expert Contributor
Are you certain that $position has a number in it? Have you checked the value of $position?

--Kevin
Jul 17 '08 #4
numberwhun
3,509 Recognized Expert Moderator Specialist
Are you certain that $position has a number in it? Have you checked the value of $position?

--Kevin
To find out, you could run your script in the Perl debugger and check all the values to make sure. To start the debugger, use:

Expand|Select|Wrap|Line Numbers
  1. perl -d <scriptname>
  2.  
THe debugger is a little daunting at first, but its not bad once you read a tutorial, even the one at Perldoc is doable.

Regards,

Jeff
Jul 17 '08 #5
nithinpes
410 Recognized Expert Contributor
Are you certain that $position has a number in it? Have you checked the value of $position?

--Kevin

sessmurda,

I was wondering about the logic in this loop:
Expand|Select|Wrap|Line Numbers
  1.     foreach ($position) {
  2.         while ($postion > $orf_beg) {
  3.         print "$position $ref_seq $para_seq $baya_seq\n";
  4. }}
  5.  
  6.  
Inside while ($postion > $orf_beg) {} loop, you are not modifying the value of $position. So, if the condition returns true, this will result in an infinite loop.

You should be using if() loop instead of while(). However, check if $position has indeed a number in it by printing out the value.

- Nithin
Jul 18 '08 #6
KevinADC
4,059 Recognized Expert Specialist
this line:

Expand|Select|Wrap|Line Numbers
  1. $position = substr $_, 1, 11;
with this input fromthe file:

Expand|Select|Wrap|Line Numbers
  1. 662376 [S] |GCGG | | |
will result in:

62376 [S] |

Is that really what you want?
Jul 18 '08 #7
numberwhun
3,509 Recognized Expert Moderator Specialist
sessmurda,

I was wondering about the logic in this loop:
Expand|Select|Wrap|Line Numbers
  1.     foreach ($position) {
  2.         while ($postion > $orf_beg) {
  3.         print "$position $ref_seq $para_seq $baya_seq\n";
  4. }}
  5.  
  6.  
Inside while ($postion > $orf_beg) {} loop, you are not modifying the value of $position. So, if the condition returns true, this will result in an infinite loop.

You should be using if() loop instead of while(). However, check if $position has indeed a number in it by printing out the value.

- Nithin
Also, correct me if I am wrong, but I don't see that $position was defined as an array, so doing a foreach will result in one time through.

Regards,

Jeff
Jul 18 '08 #8
nithinpes
410 Recognized Expert Contributor
Also, correct me if I am wrong, but I don't see that $position was defined as an array, so doing a foreach will result in one time through.

Regards,

Jeff
Thanks for the addition, Jeff. I missed it totally :)

The foreach loop will be of no specific purpose in that section of script.
Jul 18 '08 #9
sessmurda
14 New Member
I am currently working on the foreach and while/if suggestions you guys provided, thanks! To answer some questions: the position is a number, it doesnt show when I copy and pasted the text, but there is a lot of white space between the last number of the position and the sequence character to allow for the fact that on occasion there will need to be decimals due to the format of the file. I checked all of the substring column numbers before posting, they are all correct. I originally had the if statement inside the loop but it kept asking me for the polymorphism on every iteration, I incorrectly thought while was what I should be using. Also when I tried pushing the $position variable into an array it would not move past the first position, even when I tried the shift function. When I use the script I posted without the while/if line, and have the $position being acted on by the foreach loop, it works, its just that I want to catalogue the positions based on where I specify it, then output those to each their own file, which I will work on after I get the script working.

Thanks again for the help.
Jul 18 '08 #10

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

Similar topics

6
1671
by: Dariusz | last post by:
I have a database where I use the while() command to iterate through each row and their resultant arrays to populate a table as the while() command is executed. I would like to have in the database a "pointer" that says something like $MyString, but in the PHP $MyString is given a URL of an image. So every time during the while() command it comes across the text $MyString it instead substitutes it for the URL instead of echoing...
4
15953
by: muser | last post by:
I have a logical error in my program, I have submitted the program and my tutor hasn't listed the other problems with the code, but said that the program won't run because of a while statement. The while statement is as follows. while(infile.peek() != EOF) { infile.getline(temp1, max); };
2
7806
by: Jim | last post by:
Im getting way too many rows retured..what its trying to do is insert a 0 for revenue for months 7 - 12 (aka July through December) for each of these cost centers for each payor type..Im getting a lot of repeats and the concatenation field date always comes back as January 2003 instead of the month and date its supposed to --Fiscal Year
147
10082
by: Michael B Allen | last post by:
Should there be any preference between the following logically equivalent statements? while (1) { vs. for ( ;; ) { I suspect the answer is "no" but I'd like to know what the consensus is
12
1972
by: Howard | last post by:
Hello everyone (total VB.NET beginner here), I'm reading the "SAMS Teach Yourself VB.NET In 21 Days" book, and came across an exercise that I can't get to work. The exercise asks that you create a game that makes the user guess a number from 1-100, and you tell the user "lower" or "higher" as they input their guesses, until they guess the correct number, at which point you then tell the user "Correct". I tried using the While Loop, and...
1
2064
by: pantagruel | last post by:
Hi, I have an array like the following: for(x=0;x<results.length;x++){ alert(results.length); extracted=results.shift(); alert(results.length); if(results.indexOf(extracted)== -1){
4
1345
by: devilsangels287 | last post by:
Hey guys I have a problem with this while loop, its not working properly and I think that I might be typing it wrong but I don't how. please help me! Thanks... Anyways this is a calculator which is suppose to find the approximated square root value and using this algorithm, it is suppose to work. However in the while loop it apparently doesn't work for me. Heres the code I put in: #include <stdio.h> int main () {
5
1564
by: Shawn Minisall | last post by:
I just learned about while statements and get why you place them around inputs for validation, but I'm a little lost on exactly where to place it with what condition in this program where the number of fat grams exceeds the total number of calories so that it loops back and asks you the two questions again instead of just saying The calories or fat grams were incorrectly entered. Any idea's? thx while cal <=0:
6
2415
by: Shawn Minisall | last post by:
I've been having some problems with using a while statement for one menu within another while statement for the main menu, first time I've done it. It's with choice number two from the menu. When I run the program, I get a UnboundLocalError: local variable 'ai' referenced before assignment. I initialize ai as "", but then it just skips to the you choose scissors I choose and nothing shows up. As soon as I take away the while again ==...
0
8352
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8863
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8636
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7378
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6189
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4192
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4358
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2765
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2005
muto222
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.