473,387 Members | 1,495 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

find the highest number in a array

i wonder am i make a mistake or not...
Expand|Select|Wrap|Line Numbers
  1. use Data::Dumper;
  2.  
  3. my @array= qw(1 2 3 4 5 6 8 9 2 5);
  4.  
  5. my $highest=@array[0];
  6.  
  7. foreach my $number (@array) {
  8.     if (@array[$number] > $highest) {
  9.     $highest = @array[$number];
  10.     }
  11. }
  12. print Dumper $highest;
i get the output as $VAR1 = 8;

suppose the answer should be 9 right?
Jul 23 '08 #1
3 10567
numberwhun
3,509 Expert Mod 2GB
If you are looking for the last element in the array, you can get that with the special variable:

Expand|Select|Wrap|Line Numbers
  1. $#arrayname
  2.  
On the other hand, take that number and add one and you have the number of elements in the array (not starting at zero).

Regards,

Jeff
Jul 23 '08 #2
nithinpes
410 Expert 256MB
The problem lies in this section of your script:
Expand|Select|Wrap|Line Numbers
  1. foreach my $number (@array) {
  2.     if (@array[$number] > $highest) {
  3.  
foreach loop will fetch each element of the array, not each index. So, the comparison section should be:
Expand|Select|Wrap|Line Numbers
  1. foreach my $number (@array) {
  2.     if ($number > $highest) {
  3.     $highest = $number;
  4.     }
  5. }
  6.  
Jul 23 '08 #3
KevinADC
4,059 Expert 2GB
use sort function, which will be much faster anyway:

Expand|Select|Wrap|Line Numbers
  1. my @array= qw(1 2 3 4 5 6 8 9 2 5);
  2. print ((sort {$b <=> $a} @array)[0]);
Jul 23 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Massimiliano Alberti | last post by:
Can someone check this? If it's OK, you can use however you want... :-) It should search for an element in an array and, if it can't find it, return the next element. key is what you are...
21
by: Jaspreet | last post by:
I was working on some database application and had this small task of getting the second highes marks in a class. I was able to do that using subqueries. Just thinking what is a good way of...
13
by: mike | last post by:
I have ListArray with number in Eg: 1, 1.456, 2.43, 4, 6.78 next i have a decimal variable containing one number EG: 1.786 Could someone please tell me how i find the "closest match" number...
5
by: Ada | last post by:
hello folks, just wanna get some feedback from someone here who has more experience. :-) i'm developing a small app that require searching for the highest index within a directory. i was...
7
by: Jan | last post by:
Hi there, Is there a fast way to get the highest value from an array? I've got the array strStorage(intCounter) I tried something but it all and's to nothing If someone good helpme, TIA
21
by: Imran | last post by:
I have a vector of integers, such as and I want to find out the number which occurs most frequently.what is the quick method. My array size is huge. what I am doing is 1. find out the...
3
by: lostncland | last post by:
I am working on this program. The array has 10 scores. (there is more to this program.) Does the last "for" section make sense? /*Defines a global constant called N throughout the file. ...
17
by: rhitz1218 | last post by:
Hi, I'm trying to create a function that will sort a number's digits from highest to lowest. For example 1000 - will become 0001 or 1234 to 4321
13
by: td0g03 | last post by:
Hello again guys. I have a question. I'm working on a program for my class and I'm stuck on how to find the lowest and highest number of a user input. We aren't at arrays yet so that's out of the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.