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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Issue with Nested IF loops

Status
Not open for further replies.

NesPes

Programmer
Dec 15, 2007
17
US
I face issues when i have nested if conditions. The code never goes to the second nested if loop. Any ideas/thoughts on this?
 
Who knows hungry, maybe if we just meditate long enough the answer will come to us from the ether.

[orientalbow]

NesPes, as he says, without seeing your code it's practically impossible for us to predict what you're doing wrong. However, my random guess is that you are using an assignment in your if condition instead of an equality operator. Make sure that you use two equal signs to testing numerical equality, not just one, otherwise the first condition will always return true.

- Miller
 
I have foll code:

while (<G1>) {
if ($_ =~ /Some/)
{
{
do something;
} -----> End first loop
if ($_ =~ /More/)
{
do something;
}
} -----> End second loop
} ----> End of while loop
 
@test = qw(test Some More SomeMore);
for (@test) {
if ($_ =~ /Some/){
print "Got Some\n";
}

if ($_ =~ /More/) {
print "Got More\n";
}

}

print "\n\n";

#or maybe you meant

for (@test) {
if ($_ =~ /Some/){
print "Got Some\n";
if ($_ =~ /More/) {
print "Got some and more\n";
}

}

}



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
NesPes,

you have not posted code with nested loops. Most likely what you want is to use if/elsif blocks:

Code:
while(<>) {
   if ( condition ) {
     ...
   }
   elsif ( condition ) {
      ...
   }
   #you can have more eslif conditions here 
   else {
      fall through operation
   }
}



------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
My task involves me to check for a particular string in a file and capture some part in a variable. I then have to use this variable value and search for the remainder part of the file. If it is found, i have to do some processing.
It is much like any sorting example. So, i guess i cannot replace them with elsif statements.
 
Unless you provide an example of your data and what you are trying to do it will be near impossible to help you anymore.

One thing that is throwing everyone off (or maybe just me) is your incorrect use of terminology in relationship to perl coding.

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

Part and Inventory Search

Sponsor

Back
Top