Finding Max value from an array | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Finding Max value from an array

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-31-2009
vjasai vjasai is offline
Registered User
 
Join Date: Jul 2009
Last Activity: 2 August 2009, 10:57 AM EDT
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
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
Sponsored Links
    #2  
Old 07-31-2009
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
 
Join Date: Mar 2005
Last Activity: 5 July 2014, 11:14 AM EDT
Location: classification algos
Posts: 3,203
Thanks: 19
Thanked 30 Times in 27 Posts
Post what you had tried so far.
Sponsored Links
    #3  
Old 07-31-2009
vjasai vjasai is offline
Registered User
 
Join Date: Jul 2009
Last Activity: 2 August 2009, 10:57 AM EDT
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
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
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
 
Join Date: Mar 2005
Last Activity: 5 July 2014, 11:14 AM EDT
Location: classification algos
Posts: 3,203
Thanks: 19
Thanked 30 Times in 27 Posts
Thanks for posting what you have tried

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

Sponsored Links
    #5  
Old 07-31-2009
Scott's Avatar
Scott Scott is offline Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 22 September 2014, 1:14 AM EDT
Location: Zürich
Posts: 8,483
Thanks: 332
Thanked 1,047 Times in 914 Posts
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

Sponsored Links
    #6  
Old 07-31-2009
sharadpisal sharadpisal is offline
Registered User
 
Join Date: May 2008
Last Activity: 6 December 2011, 11:03 AM EST
Posts: 41
Thanks: 0
Thanked 1 Time in 1 Post
use sort -nr
'n' will take care to sort it numerically
Sponsored Links
    #7  
Old 07-31-2009
Scott's Avatar
Scott Scott is offline Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 22 September 2014, 1:14 AM EDT
Location: Zürich
Posts: 8,483
Thanks: 332
Thanked 1,047 Times in 914 Posts
Leading 0's will take care of that...
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Creating an array to hold posix thread ids: Only dynamic array works kmehta Programming 4 09-21-2008 08:24 PM
split and making an array inside another array dcfargo Shell Programming and Scripting 2 08-06-2008 10:07 AM
Array inside an array manas_ranjan UNIX for Advanced & Expert Users 5 06-10-2008 02:25 PM
create array holding characters from sring then echo array. rorey_breaker Shell Programming and Scripting 5 09-28-2007 08:42 AM
finding duplicate files by size and finding pattern matching and its count jerome Sukumar Shell Programming and Scripting 2 12-01-2006 03:20 AM



All times are GMT -4. The time now is 02:13 AM.