Finding Max value from an array


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
Old 07-31-2009
Finding Max value from an array

Hi,

I need to find max and second max element from an array.
array contains 0338,0337,0339,0340,0401,0402,0403
# 3  
Old 07-31-2009
typeset currel_max=0

for i in `echo ${array[*]}`
do
if [ $currel_max -lt $i ]
then
currel_max=$i
fi
done

so was succesful in finding max value. but want to find second max value.

Also want to know if there is any other way to find.. instead of using for loop.
# 4  
Old 07-31-2009
Thanks for posting what you have tried Smilie

One more variable and keep flipping - but not efficient

Code:
awk 'BEGIN{ max = -1; second_max = -1 }{ if ( $0 > max ) { second_max = max; max = $0 } }END{ print second_max }' file

# 5  
Old 07-31-2009
As an alternative:

Code:
set -A MyArray 0338,0337,0339,0340,0401,0402,0403
echo ${MyArray[@]} | awk -v RS="," '1' | sort -r | head -2

0403
0402

# 7  
Old 07-31-2009
Leading 0's will take care of that...
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Finding null column value using array rogerben Shell Programming and Scripting 4 06-09-2015 05:08 AM
Php number array from max, min, step size mysql data barrydocks Programming 6 11-18-2014 11:23 PM
Finding max of a column grouping by the time satishrao Shell Programming and Scripting 3 03-18-2014 01:45 AM
Finding difference in between two array's of strings Y.balakrishna Shell Programming and Scripting 3 12-05-2013 06:50 AM
awk, max value, array, row yifangt Shell Programming and Scripting 1 07-16-2012 02:59 PM
Finding max number in filename and opening it rpd25 Shell Programming and Scripting 8 12-28-2011 03:25 AM
finding max size Diya123 Shell Programming and Scripting 9 09-14-2011 10:25 PM
Finding indices in an array nearest to a set of values kristinu Shell Programming and Scripting 7 07-13-2011 12:04 AM
Finding range of values in an array kristinu Programming 3 11-29-2010 11:16 PM
max limit in in setting array gomathi HP-UX 2 05-07-2010 10:38 AM
Max amount of awk array indices timj123 Shell Programming and Scripting 0 06-19-2008 06:51 PM
Help in finding the max and min position ahjiefreak Shell Programming and Scripting 1 06-08-2008 07:32 AM
MAX SIZE ARRAY Can Hold it epall UNIX for Advanced & Expert Users 3 06-07-2006 05:05 AM
Finding max value Enigma23 Shell Programming and Scripting 1 04-10-2005 08:34 AM