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: Intermediate:
Checking for largest number

 



Warren Bell
Deleted

May 14, 2000, 6:20 PM

Post #1 of 6 (1428 views)
Checking for largest number Can't Post

I've got a part of a script that opens an HTML page and looks for a set of lines with numbers in them. Like

starts the same 3
starts the same 2
starts the same 7

How can I search for the lines that start the same and determine which line has the largest number? Then be able to print that number.


Cure
User

May 16, 2000, 8:38 AM

Post #2 of 6 (1428 views)
Re: Checking for largest number [In reply to] Can't Post

Hi

my $highest =0;
while(<DATA> ) {
if( /starts the same (\d*)/ ) {
$highest = $1 if $1 > $highest;
}
}
print "$highest";


Cure


Warren Bell
Deleted

May 16, 2000, 11:22 AM

Post #3 of 6 (1428 views)
Re: Checking for largest number [In reply to] Can't Post

That didn't work. It prints out the $highest over an over again, about 50 times. If I have my $highest=0; it prints out the highest as 0. If I leave out the my $highest it gets the correct number but prints it out 50 times.

Here's my code below. How can I stop it from printing multile times? I need the for (n=0;...) for other things I search for. I even tried remove the while(){} and it still works but still prints out about 50 times.

------------------------------
open (INFO, "$newspage") &#0124; &#0124; &fatal_error("Unable to open $newspage");
&lock(INFO);
@npage = <INFO>;
close(INFO);

# this code is here already for other stuff I check for
for ($n = 0; $n < @npage; $n++) {
$_ = $npage[$n];

while(<DATA> ) {
if (/<a name.*(\d*).*<\/table><br>/) {
$highest = $1 if $1 > $highest;
}
}

print "Top: $highest";

}


nagao
Deleted

May 16, 2000, 2:00 PM

Post #4 of 6 (1428 views)
Re: Checking for largest number [In reply to] Can't Post

You have your print statement in the for loop. Make sure you initialize $highest and do your print outside of those loops.

Nick


Cure
User

May 16, 2000, 3:31 PM

Post #5 of 6 (1428 views)
Re: Checking for largest number [In reply to] Can't Post

My Code does work, the problem is that you do not give us enough information to help you

You haven't told us if "starts the same" has the possiblity of having
any numbers in it, let's assume it does not. You haven't told us
whether the numbers you are looking for are digits, whole numbers,
floating point numbers, or what. Let's assume they are whole numbers.
Given those assumptions:

Cure



Warren Bell
Deleted

May 16, 2000, 3:41 PM

Post #6 of 6 (1428 views)
Re: Checking for largest number [In reply to] Can't Post

It works after I moved the print out of the loop. Thanks, I appreciate it.

 
 


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

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