?
Solved

Print Only First Three Highest Values

Posted on 2007-11-16
5
Medium Priority
?
194 Views
Last Modified: 2013-12-25
Hi All

I have 8 variables with numeric values:

$Variable1 = 80;
$Variable2 = 50;
$Variable3 = 30;
$Variable4 = 70;
$Variable5 = 20;
$Variable6 = 50;
$Variable7 = 40;
$variable8 = 10;


How can I print only the first three with the highest value.

EX:

print "$Variable1\n $Variable4\n $Variable2\n";
0
Comment
Question by:blnukem
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 3

Expert Comment

by:BioI
ID: 20297591
It is better to store them in an array I think

@array = (80, 50, 30, 70, 20, 50, 40, 10);
@sorted = sort { $b <=> $a } @array;

the highest number is now stored at index 0
print "highest: ", $sorted[0], "\n";
print "second highest: ", $sorted[1], "\n";
...

remarkt that if you do {$a <=> $b}, you sort in an ascending way.
with the current {$b <=> $a}, you sort in a descending way
0
 
LVL 84

Accepted Solution

by:
ozo earned 2000 total points
ID: 20297592
print "$_\n" for (sort $Variable1,$Variable2,$Variable3,$Variable4,$Variable5,$Variable6,$Variable7,$Variable8)[-3..-1]
0
 

Author Closing Comment

by:blnukem
ID: 31409535

Thanks ozo!!
0
 
LVL 3

Expert Comment

by:BioI
ID: 20297632
@ozo,

the sort function - as far as I know, sorts alphabetically, and your solution will run into problems.
But if you change e.g. $variable8=10 into $variable8=150, your code will still return 50, 70, 80 because it sorts them alphabetically
same problem if you change variable8 from 10 to 9: your solution will then return 9 as the highest number (higher than 80).
0
 
LVL 84

Expert Comment

by:ozo
ID: 20297646
> sorts alphabetically
true
(sort {$b<=>$a} $Variable1,$Variable2,$Variable3,$Variable4,$Variable5,$Variable6,$Variable7,$Variable8)[0..2]
would have been better
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question