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:
Quick way to find biggest number of three?

 



Warren Bell
Deleted

Sep 1, 2000, 10:14 PM

Post #1 of 7 (745 views)
Quick way to find biggest number of three? Can't Post

What's the quickest way to find the biggest number if I have three variables containing numbers? Like if I have:

$num1 that contains 10
$num2 that contains 15
$num3 that contains 20

Is there somthing I can do that will return the largest without having to do:

if ($num1 >= $num2) {
if ($num1 >= $num3) {
.....
}
}


Kanji
User

Sep 2, 2000, 12:06 PM

Post #2 of 7 (745 views)
Re: Quick way to find biggest number of three? [In reply to] Can't Post

Not sure if it's the quickest, but ...

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

($biggest) = sort { $b <=> $a } ($num1,$num2,$num3);</pre><HR></BLOCKQUOTE>


Warren Bell
Deleted

Sep 2, 2000, 2:10 PM

Post #3 of 7 (745 views)
Re: Quick way to find biggest number of three? [In reply to] Can't Post

Nice, thanks!


Warren Bell
Deleted

Sep 2, 2000, 2:49 PM

Post #4 of 7 (745 views)
Re: Quick way to find biggest number of three? [In reply to] Can't Post

The only thing is is I need to remember which one was the highest by it's name so I can print somthing. Here's what I use:

if (($int >= $fun) && ($int >= $off)) {
print FILE "text";
}
elsif (($fun >= $int) && ($fun >= $off)) {
print FILE "text2";
}
elsif (($off >= $fun) && ($off >= $int)) {
print FILE "text3";
}


Kanji
User

Sep 2, 2000, 9:58 PM

Post #5 of 7 (745 views)
Re: Quick way to find biggest number of three? [In reply to] Can't Post

In such situations, I find using arrays or hashes much more useful as you don't have to perform contortions to work out the original name.
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

%vars = (
"fun" => 10, # $vars{'fun'} = 10
"int" => 20, # $vars{'int'} = 20
"off" => 15, # $vars{'off'} = 15
);
($biggest) = sort { $vars{$b} <=> $vars{$a} }
keys %vars;

print "$biggest = $vars{ $biggest }";
__END__
int = 20</pre><HR></BLOCKQUOTE>

Then, if you had another hash with the same keys, but the text you wanted to print you could simply do ...

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

print FILE $text{ $biggest };</pre><HR></BLOCKQUOTE>

... which gets rid of all those if/elses entirely.


Warren Bell
Deleted

Sep 3, 2000, 9:43 AM

Post #6 of 7 (745 views)
Re: Quick way to find biggest number of three? [In reply to] Can't Post

Thanks that works perfect.


japhy
Enthusiast

Sep 5, 2000, 7:50 AM

Post #7 of 7 (745 views)
Re: Quick way to find biggest number of three? [In reply to] Can't Post

I think a non-hash solution is best. If you have a set of numbers, and you want the highest, then why not just use:

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


@num = qw( 17 43 12 54 12 5 3 );
$wanted = highest(\@num);

sub highest {
my ($wanted,@_) = @{ $_[0] };
for (@_) { $wanted = $_ if $_ > $wanted }
return $wanted;
}
</pre><HR></BLOCKQUOTE>

This does N-1 comparisons for a list of N elements. My tests with sort() show less than favorable results:

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


for (
[1,2,3,4], [1,2,4,3], [1,3,2,4], [1,3,4,2],
[1,4,2,3], [1,4,3,2], [2,1,3,4], [2,1,4,3],
[2,3,1,4], [2,3,4,1], [2,4,1,3], [2,4,3,1],
[3,1,2,4], [3,1,4,2], [3,2,1,4], [3,2,4,1],
[3,4,1,2], [3,4,2,1], [4,1,2,3], [4,1,3,2],
[4,2,1,3], [4,2,3,1], [4,3,1,2], [4,3,2,1]) {
sort { $j++, $b <=> $a } @$_;
$count++;
}
printf "%d sets, %d cmp's, average: %.2f\n", $count, $j, $j/$count;
</pre><HR></BLOCKQUOTE>

24 sets, 118 <=>'s, average: 4.92

The average is nearly 5, which is N+1. And only two cases had N-1 (3) comparisons. A hand-rolled solution might be the way to go.

------------------
Jeff "japhy" Pinyan -- accomplished author, consultant, hacker, and teacher


 
 


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

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