473,813 Members | 2,990 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

find the highest number in a array

72 New Member
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 10665
numberwhun
3,509 Recognized Expert Moderator Specialist
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 Recognized Expert Contributor
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 Recognized Expert Specialist
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
2231
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 searching, in a *base array of LONG with num elements. After some tests it seems to work well (the original version was filled with ASSERTs) (I will use this function in a bigger look-for-or-insert function) Some notes: The function returns the index...
21
13492
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 getting second highest value in an integer array. One method I know of is to make the 1st pass through the array and find the highest number. In the second pass we can find the highest number which is less than the number we obtained in the 1st pass.
13
12330
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 below the decimal variable from the arraylist. Thanks ever so much in advance
5
2349
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 thinking of comparing string. not sure if this is the most efficient way to do it.
7
3360
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
8156
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 maximum value N 2. loop through 1...N 3. count # times each occurred
3
16104
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. Note that N = 10. */ #define N 10 #include <iostream> using namespace std;
17
7728
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
141315
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 question, but what would be another way of finding the highest and lowest. I remember hearing something in the lines of "cout <<" in class, but forgot. Also I could try the if else statement, but would seem like alot of work. I hope I am making this...
0
9734
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10408
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10426
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9225
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7686
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6897
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5570
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4358
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3886
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.