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:
Help in while loop

 



datlaravi
Deleted

Jun 23, 2000, 5:30 AM

Post #1 of 11 (1538 views)
Help in while loop Can't Post

Hi,
I need some help in the while loop.
When i use while loop ($sybHandle->fetchrow)
it is going to every record in the database.
if i use (If condition and else condition) in a while loop,it is not working.

I want to check a value
for example, if ($over==1), i want to print the records and if ($over==0), i want to print "there in no match found.
It is printing for every record
both the conditions because of $sybHandle->fetchrow

Here is the code
sub getoverhead {
$loc = $query->param('locationName');
$select ="select location,conferenceRoomName,overHead,seats from ConferenceRooms where location ='$loc'";
$sybHandle = $syb->prepare($select);
$sybHandle->execute();
while (my ($loc,$conf, $over,$seats) = $sybHandle->fetchrow){
if ($over==1) {
print <<"html";
<table><tr><td>$loc</td><td>$conf</td><td>$seats</td></tr>
</table>
html
}
if ($over==0) {
print "There is no match\n";
}
}
}
Because of the $sybHandle->fetchrow,
it is printing for everyrecord,if it is one, it is printing the results and if it is 0, it is printing "there is no match"
I want to print the result only if ($over==1)
or if ($over==0)
How do i get the records without $sybHandle->fetchrow.
Please help me


DrZed
User

Jun 23, 2000, 8:55 AM

Post #2 of 11 (1538 views)
Re: Help in while loop [In reply to] Can't Post

Clarification is required. You say that it's doing something and then state that you want it to do exactly what it's doing.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">quote:</font><HR>
it is printing for everyrecord
if it is one, it is printing the results
if it is 0, it is printing "there is no match"
<HR></BLOCKQUOTE>

Ok.
If 1, print result.
If 0, print no match.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">quote:</font><HR>
I want to print the result only if ($over==1)
or if ($over==0)
<HR></BLOCKQUOTE>

How is this different from what it is doing?

Dr. Zed


datlaravi
Deleted

Jun 23, 2000, 9:36 AM

Post #3 of 11 (1538 views)
Re: Help in while loop [In reply to] Can't Post

sorry, i didn't state it properly.

What i am trying to do is, print the result if($over ==1). It is going to print the record if $over value is 1 in the database. If the value is 0 in the database, it should print "There is no match found."

The code above is printing as

215 street main floor 20
There is no match
215 street 2nd floor 15
There is no match
215 street 3rd floor 10

for every record, if the value is 1, it is printing the record.(1st line). If the value is 0, it is printing "there is no match"(2nd line)
Hope this clarifies.
Please help me


Kanji
User / Moderator

Jun 23, 2000, 4:49 PM

Post #4 of 11 (1538 views)
Re: Help in while loop [In reply to] Can't Post

What happens when you print out $over? And why aren't you using a if ( ) { ... } else { ... } loop instead of two if's?


DrZed
User

Jun 23, 2000, 5:06 PM

Post #5 of 11 (1538 views)
Re: Help in while loop [In reply to] Can't Post

I'm still lost.

You posted an example of what it is doing:
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">quote:</font><HR>
215 street main floor 20
There is no match
215 street 2nd floor 15
There is no match
215 street 3rd floor 10
<HR></BLOCKQUOTE>

Could you post an example of what you want it to do.

Dr. Zed


japhy
Enthusiast

Jun 25, 2000, 6:05 AM

Post #6 of 11 (1538 views)
Re: Help in while loop [In reply to] Can't Post

His problem is a logic problem. Let's make a simple case. Your data file looks like:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


M--18--Jeff
M--24--Chris
F--18--Kristin
M--51--Charles
F--13--Sarah
</pre><HR></BLOCKQUOTE>

And you want to extract males over 20. So you make a loop like:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


while (<PEOPLE> ) {
chomp;
($sex,$age,$name) = split /--/;
if ($sex eq "M" and $age >= 20) {
print "$name is male and is $age years old\n";
}
else {
print "No male found over 20 years old.";
}
}
</pre><HR></BLOCKQUOTE>

But that gives us the current problem. The output of this program is:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


No male found...
Chris is male and is 24 years old
No male found...
Charles is male and is 51 years old
No male found...
</pre><HR></BLOCKQUOTE>

Obviously, this is happening because the else-block is getting reached when a single record is found to be a non-match. We want it called if the if-block NEVER gets called (meaning EVERY record didn't match). To get around this problem, do:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


$found = 1;
while (<PEOPLE> ) {
chomp;
($sex,$age,$name) = split /--/;
if ($sex eq "M" and $age >= 20) {
print "$name is male and is $age years old\n";
$found = 1;
}
}

if (!$found) {
print "No male found over 20 years old.\n";
}
</pre><HR></BLOCKQUOTE>

Ta da.


datlaravi
Deleted

Jun 25, 2000, 10:22 PM

Post #7 of 11 (1538 views)
Re: Help in while loop [In reply to] Can't Post

Thanks !!! it worked.


datlaravi
Deleted

Jun 26, 2000, 6:20 AM

Post #8 of 11 (1538 views)
Re: Help in while loop [In reply to] Can't Post

Hi,
Thanks for the help.
I tried using $found =1. It is working, but the second if statement is not executing.
For example, if there is no match, it is appearing blank, instead of "No male found over 20 years old"
if (!found) {
"No male found over 20 years old";
}
the above is statement is not getting executed.
The output is
If there is a match,
"John is male and 25 years old.
It works fine,
if there is no match,
nothing is happening.

Please help me


japhy
Enthusiast

Jun 26, 2000, 6:50 AM

Post #9 of 11 (1538 views)
Re: Help in while loop [In reply to] Can't Post

I'm sorry, that was a typo on my part. I initialized $found to 1 at the very beginning, and I meant for it to be 0. It should work for you now.


datlaravi
Deleted

Jun 26, 2000, 7:50 AM

Post #10 of 11 (1538 views)
Re: Help in while loop [In reply to] Can't Post

Hi,
Thanks a lot.
It is working. Can you help me a bit on this.
if($over ==1),I want to print the heading
"The following rooms satisfy your search and
search criteria".
where should i use this? if i use after the while loop, it is printing for every record.
I want to print the heading only once.
Right now, the output is
'john is male and 21 years old'
I want to print
The following matched your search
John is male and 21 years old.
if i add in the subroutine,
while (my ($loc,$conf, $over,$seats) = $sybHandle->fetchrow){
if($over==1) {
print <<"html";
The following matched your search
<table><tr><td>$loc</td><td>$conf</td><td>$seats</td></tr>
</table>
html
}
}
The line is printing for every record.
Please help me.
I appreciate your help


monocle
User

Jun 26, 2000, 9:01 AM

Post #11 of 11 (1538 views)
Re: Help in while loop [In reply to] Can't Post

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


$found = 0;
while (<PEOPLE> ) {
chomp;
($sex,$age,$name) = split /--/;
if ($sex eq "M" and $age >= 20) {
if (!$found) {
print "The following satisfy your search and search criteria:\n";
}
print "$name is male and is $age years old\n";
$found = 1;
}
}

if (!$found) {
print "No male found over 20 years old.\n";
}
</pre><HR></BLOCKQUOTE>


------------------
Monocle
Hear great techno music by Monocle at http://www.mp3.com/monocle. CD now on sale!



[This message has been edited by monocle (edited 06-26-2000).]

 
 


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

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