Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loop Problem 2

Status
Not open for further replies.

torstens

Technical User
Oct 9, 2006
26
US
I'm trying perl for the first time and I just can't seem to get loops to work, I've copied a loop from O'Reilly's "Learning Perl" that doesn't work for me. I'm running active perl on a windows xp system...

#!usr/bin/perl

#right out of O'Reilly's "Learning Perl"

my $some_other = "I dream of betty rubble.";

if ($some_other =~ /\brub/) {
print "Aye, there's the rub.\n;
}

<STDIN>; #so that the window stays open
 
You're missing the closing quote from your print statement.
 
Thanks much, let me rephrase this code...

I'm trying perl for the first time and I just can't seem to get loops to work. What's wrong with this? I thought it should place whatever the =~ finds into the @results. Any help is appreciated.


sequence = "ATGCATGCTAGTAGCTGATGCTGTAGCACATCAACGATGC"

while (@sequence =~ /CAT(.*?)GCT/) {

push @results, $1;

}

print @results;



<STDIN>;
 
Sorry for all the updates...

I'm trying perl for the first time and I just can't seem to get loops to work. What's wrong with this? I thought it should place whatever the =~ finds into the @results, but nothing is returned. Any help is appreciated.


$sequence = "ATGCATGCTAGTAGCTGATGCTGTAGCACATCAACGATGC";

while ($sequence =~ /CAT(.*?)GCT/) {

push @results, $1;

}

print @results;



<STDIN>;
 
sequence" shouldn't be an array: it should be a scalar:
Code:
my $sequence = "ATGCATGCTAGTAGCTGATGCTGTAGCACATCAACGATGC"

while ($sequence =~ /CAT(.*?)GCT/) {

   push @results, $1;

}

print @results;



<STDIN>;
 
There are other errors too: you're missing a semicolon from the line where you define $sequence. Also, the regexp should probably have a /g at the end of it.

Are you sure $sequence is as given in the book? As far as I can tell, your regexp will only match one part of the string (and at that point, $1 will be an empty string).
 
Your regexp needs the /g modifier to run in a while loop, otherwise it's just re-evaluating the rexexp from scratch... ie.. it's ALWAYS true (endless loop, followed by possible out of memory error) or ALWAYS false (empty array).

.. the regexp would be /CAT(.*?)GCT/g
 
Thanks for the help, now I'm trying to get the array to print a list, but it isn't working. Any ideas as far as what I'm doing wrong?

$sequence = AAA1TTT2AAA3TTT4AAA5TTT6AAA7TTT8AAA9TTT;

while ($sequence =~ /AAA(.*?)TTT/g) {

push @results, $1 \n;

}

foreach $results (@results) {
$results .= "\n";
}


print @results;

<STDIN>;
 
Not sure what you mean by "print a list" - can you elaborate on what's happening that you're not expecting, and what you're expecting to happen that isn't happening?

BTW you're still declaring your $sequence scalar incorrectly: you need quotes around the string.

 
Code:
sequence = 'AAA1TTT2AAA3TTT4AAA5TTT6AAA7TTT8AAA9TTT'; 
 
@results = $sequence =~ /AAA(.*?)TTT/g; 
$_.="\n" for @results; 
print @results;




- Kevin, perl coder unexceptional!
 
You need quotes around both the string sequence and around the value that you are pushing to the @results array (since it contains a newline (\n) character:

Code:
$sequence = "AAA1TTT2AAA3TTT4AAA5TTT6AAA7TTT8AAA9TTT";

while ($sequence =~ /AAA(.*?)TTT/g) {
  push @results, "$1\n";
}

print @results;

<STDIN>;

Now, because there is only one command in the while construct, it can be re-written to be a little more 'natural language' readable:

Code:
$sequence = "AAA1TTT2AAA3TTT4AAA5TTT6AAA7TTT8AAA9TTT";

push @results, "$1\n" while ($sequence =~ /AAA(.*?)TTT/g);

print @results;

<STDIN>;
 
Now what if I was drawing the sequence data from a sting of text from a text file. After I place it into a string, how would I make it a scalar. aka, essentially placing single quotes on each side? Should I be more detailed?
 
Code:
open(FILE,"yourfile.txt") or die "can't open the file: $!";
while (my $sequence = <FILE>) {
   #do something here with $sequence
}
close(FILE);

- Kevin, perl coder unexceptional!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top