CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Problem with condition statement

 



kwatts59
Novice

Dec 12, 2012, 11:18 AM

Post #1 of 5 (632 views)
Problem with condition statement Can't Post

A real noobie question for you gurus.
For some reason the body of my while loop is not getting executed. Below is an abbreviated version of my program.

open(INFILE,$filename) || die "Cannot open file $filename\n";
$nohits_pos = -1;
while ((<INFILE>) && ($nohits_pos lt 0))
{
$line = $_;
print "$line";
}
close(INFILE);

I've tried all sorts of things. I tried spelling out "and" instead of using the "&&", i tried using the less than symbol "<" instead of "lt".

The program works fine if I just use
while (<INFILE>)
but I need the $nohits_pos variable so that I can exit out of the while loop.

Any help would be appreciated.


FishMonger
Veteran / Moderator

Dec 12, 2012, 11:32 AM

Post #2 of 5 (628 views)
Re: [kwatts59] Problem with condition statement [In reply to] Can't Post

The code snippet you posted never increments $nohits_pos so using that var doesn't make any sense and isn't needed.

You can simply use the 'last' function when you need to exit out of the loop prior to the end.

See: perldoc -f last
http://perldoc.perl.org/functions/last.html


(This post was edited by FishMonger on Dec 12, 2012, 11:32 AM)


kwatts59
Novice

Dec 12, 2012, 11:43 AM

Post #3 of 5 (625 views)
Re: [FishMonger] Problem with condition statement [In reply to] Can't Post

Here is a more extended version of my code.
The program is supposed to scan the input file. In the event the program encounters a line containing
"***** No hits found *****", the $nohits_pos variable will get set to something other then -1 and while loop should exit.
Unfortunately, the program never enters the while loop.

$NOHITS = "***** No hits found *****";
$nohits_pos = -1;
while ((<INFILE>) && ($nohits_pos lt 0))
{
$line = $_;
$tagpos = index($line,$TAG);
$nohits_pos = index($line,$NOHITS);
if ($nohits_pos >= 0)
{ print OUTFILE "$NOHITS\n"; }
elsif ($tagpos >= 0)
{
$line = <INFILE>; # skip next line
$line = <INFILE>; # skip next line
$top_hits[$i] = $line;
print OUTFILE "$i\t$top_hits[$i]";

while (index($line,">") < 0)
{ $line = <INFILE>; }
print OUTFILE "$i\t$line";
$line = <INFILE>;
$int_count = 1;
while ((index($line,">") < 0) and (defined $line))
{
print OUTFILE "$i\t$line";
$line = <INFILE>;
}
$nohits_pos = 1; #jump out of file
}
}


kwatts59
Novice

Dec 12, 2012, 11:56 AM

Post #4 of 5 (622 views)
Re: [kwatts59] Problem with condition statement [In reply to] Can't Post

I figured out the problem. Apparently you cant use the "and" operator with the <INFILE>. I modified the code as follows.

$NOHITS = "***** No hits found *****";
$nohits_pos = -1;
$line = <INFILE>;
while ((defined $line) and ($nohits_pos < 0))
{
$tagpos = index($line,$TAG);
$nohits_pos = index($line,$NOHITS);
if ($nohits_pos >= 0)
{ print OUTFILE "$NOHITS\n"; }
elsif ($tagpos >= 0)
{
...
$nohits_pos = 1; #jump out of while loop
}
$line = <INFILE>;
}

close(INFILE);

The program functions properly now. Thanks for all your help.
PS. Sorry about the indentation. For some reason, all my tabs get lost when I post to this forum.


FishMonger
Veteran / Moderator

Dec 12, 2012, 12:06 PM

Post #5 of 5 (620 views)
Re: [kwatts59] Problem with condition statement [In reply to] Can't Post

There are a number of problems with that code, but if it does what you want, then I'm glad I'm not the one that needs to maintain it.


Quote
PS. Sorry about the indentation. For some reason, all my tabs get lost when I post to this forum.

You need to use the 'code' tags (that's the far left button under your input box) to retain the code formatting.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives