Sponsored Content
Top Forums Shell Programming and Scripting Largest number in array. Help! Post 302610827 by chapeupreto on Wednesday 21st of March 2012 10:14:12 PM
Old 03-21-2012
Hi folks.
Quote:
You need to set big to something real, not $1. Safe to set to first value of the array.
You're right. That's the key.
Just for studying purposes, I've created two bash scripts using different for loop syntax for solving this problem.

Here we go:

Code:
#!/opt/local/bin/bash
# rod@wgo.com.br
# get the biggest number in an array of integers

declare -a list # array of integers
declare -i size # size of the array
declare -i biggest # biggest number of the array
declare -i current # current element of the array

read -a list -p "Enter each integer element separated by white spaces: " 
size=${#list[@]}

biggest=${list[0]} # first element of the array is supposed to be the biggest one
for ((i=1; i<$size; i++)); do # loop through the array ( this syntax  is kind of similar to the for loop used by the C programming language )
    current=${list[$i]} # get the current element of the array. This way  you don't have to worry too much about bash syntax for accessing  elements is an array
    if [ $current -gt $biggest ]; then
        biggest=$current
    fi
done

echo -e "\nThe biggest integer number in the array is: $biggest";

Code:
#!/opt/local/bin/bash
# rod@wgo.com.br
# get the biggest number in an array of integers

declare -a list # array of integers
declare -i biggest # biggest number of the array
declare -i current # current element of the array

read -a list -p "Enter each integer element separated by white spaces: " 
size=${#list[@]}

biggest=${list[0]} # first element of the array is supposed to be the biggest one
for current in ${list[@]}; do # loop through the array
    if [ $current -gt $biggest ]; then
        biggest=$current
    fi
done

echo -e "\nThe biggest integer number in the array is: $biggest";

Of course, there is more than a way for doing such things.
I suggest you give "Advanced Bash-Scripting Guide" a try. It's a really good and powerful book about Bash Scripting. You may download it for free here:
Code:
http://www.tldp.org/guides.html

Hope it helps.
This User Gave Thanks to chapeupreto For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

search the largest number and duplicates string

Hi, My input file contain list of username, and it may have name with number as a suffix (if duplicated). Ex: mary john2 mike john3 john5 mary10 alexa So i want to check with a specific username (without suffix number) how many duplicated name, and what is the... (13 Replies)
Discussion started by: fongthai
13 Replies

2. Shell Programming and Scripting

checking the smallest and largest number

Hi All, My script is reading a log file line by line log file is like ; 19:40:22 :INFO Total time taken to Service External Request---115ms 19:40:25 DEBUG : Batch processed libdaemon.x86_64 0-0.10-5.el5 - u 19:40:22 INFO Total time taken to Service External Request---20ms 19:40:24... (4 Replies)
Discussion started by: subin_bala
4 Replies

3. UNIX for Dummies Questions & Answers

How to print largest and smallest number.

Hey. This is pretty easy stuff but I'm learning the basics of Unix at the moment so keep that in mind. I have to: 1) Write a C-shell script to monitor user activity on the server for 13 minutes. 2) Then print the smallest and largest number of users during these 13 minutes. I have this: 1)... (2 Replies)
Discussion started by: amp10388
2 Replies

4. Shell Programming and Scripting

perl + array and incrementing number

morning guys and gals, I am haveing a problem, a friend helped me out with this script but i dont know how to add incrementing number for each movie in movie.list. this is what i have so far. any assistance would be great. I have removed the GT and LT symbols so you can see what is going on... (5 Replies)
Discussion started by: Optimus_P
5 Replies

5. Shell Programming and Scripting

How to insert the number to an array

Hi I work in ksh88 and have a file which has several "set -A " statements: set -A PNUM_LSTM 2713 4124 2635 270 2529 2259 2214 set -A PNUM_LSTM $* for pn in ${PNUM_LSTM} etc... I need to add another number, lets say 555, to the first line ONLY so only the the first line will be updated... (2 Replies)
Discussion started by: aoussenko
2 Replies

6. Shell Programming and Scripting

How to initialize array with a larger number?

Language: ksh OS: SunOS I have been getting the 'subscript out of range' error when the below array variable gets elements greater that 1024. I understand that 1024 is the default size for 'set -A' dynamic array, but is there a way to initialize it with a larger number? set -A arr `grep... (6 Replies)
Discussion started by: ChicagoBlues
6 Replies

7. Shell Programming and Scripting

awk second largest, third largest value

I have two text files like this: file1.txt: 133 10 133 22 133 13 133 56 133 78 133 98 file2.txt: 158 38 158 67 158 94 158 17 158 23 I'm basically trying to have awk check the second largest value of the second column of each text file, and cat it to its own text file. There... (13 Replies)
Discussion started by: theawknewbie
13 Replies

8. Shell Programming and Scripting

Taking largest (negative) number from column of coordinates and adding positive form to every other

Hello all, I'm new to the forums and hope to be able to contribute something useful in the future; however I must admit that what has prompted me to join is the fact that currently I need help with something that has me at the end of my tether. I have a PDB (Protein Data Bank) file which I... (13 Replies)
Discussion started by: crunchgargoyle
13 Replies

9. Shell Programming and Scripting

Locating the largest number and performing division

I have a tab delimited file with the following format 1 r 109 45 3 5 6 7 2 f 300 249 5 8 10 3 g 120 4 5 110 0 4 t 400 300 250 0 0 ..... ..... 100,000 lines I would like to get the largest number in columns 4, 5, 6, 7, 8 and divide that largest number with the number in column 3.... (4 Replies)
Discussion started by: Kanja
4 Replies

10. Shell Programming and Scripting

Reduce the number of lines by using Array

I have the following code to count the number of how many times the name occurred in one file. The code is working fine and the output is exactly what I want. The problem is the real code has more than 50 names in function listname which cause function name to have more than 50 case ,and function... (14 Replies)
Discussion started by: samsan
14 Replies
Tree::Simple::Visitor::GetAllDescendents(3pm)		User Contributed Perl Documentation	     Tree::Simple::Visitor::GetAllDescendents(3pm)

NAME
Tree::Simple::Visitor::GetAllDescendents - A Visitor for fetching all the descendents of a Tree::Simple object SYNOPSIS
use Tree::Simple::Visitor::GetAllDescendents; # create an instance of our visitor my $visitor = Tree::Simple::Visitor::GetAllDescendents->new(); # pass the visitor to a Tree::Simple object $tree->accept($visitor); # you can also get the descendents # back as an array of node values my @descendents = $visitor->getDescendents(); # for more complex node objects, you can specify # a node filter which will be used to extract the # information desired from each node $visitor->setNodeFilter(sub { my ($t) = @_; return $t->getNodeValue()->description(); }); DESCRIPTION
Given a Tree::Simple instance this Visitor will return all the descendents recursively on down the hierarchy. METHODS
new There are no arguments to the constructor the object will be in its default state. You can use the "setNodeFilter" method to customize its behavior. setTraversalMethod ($visitor) By default we will use Tree::Simple's built in depth-first (pre-order) traverse method. If however, you desire the descendents to be returned in a different ordering, this can be accomplished using a different traversal method, you can supply a $visitor object implementing that traversal type to this method (See Tree::Simple::Visitor::BreadthFirstTraversal, Tree::Simple::Visitor::PreOrderTraversal and Tree::Simple::Visitor::PostOrderTraversal). setNodeFilter ($filter_function) This method accepts a CODE reference as its $filter_function argument and throws an exception if it is not a code reference. This code reference is used to filter the tree nodes as they are collected. This can be used to customize output, or to gather specific information from a more complex tree node. The filter function should accept a single argument, which is the current Tree::Simple object. visit ($tree) This is the method that is used by Tree::Simple's "accept" method. It can also be used on its own, it requires the $tree argument to be a Tree::Simple object (or derived from a Tree::Simple object), and will throw and exception otherwise. getAllDescendents This method will give back and array of descendents in depth-first order (pre-order) or in the order specified by the "setTraversalMethod". If called in scalar context it will give an array reference, in list context it will return a regular array. This method is the same as calling "getResults". BUGS
None that I am aware of. Of course, if you find a bug, let me know, and I will be sure to fix it. CODE COVERAGE
See the CODE COVERAGE section in Tree::Simple::VisitorFactory for more inforamtion. SEE ALSO
These Visitor classes are all subclasses of Tree::Simple::Visitor, which can be found in the Tree::Simple module, you should refer to that module for more information. AUTHOR
stevan little, <stevan@iinteractive.com> COPYRIGHT AND LICENSE
Copyright 2004, 2005 by Infinity Interactive, Inc. <http://www.iinteractive.com> This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. perl v5.10.1 2005-07-14 Tree::Simple::Visitor::GetAllDescendents(3pm)
All times are GMT -4. The time now is 03:06 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy