Sponsored Content
Top Forums Shell Programming and Scripting Largest number in array. Help! Post 302610809 by Sundown on Wednesday 21st of March 2012 09:15:35 PM
Old 03-21-2012
Quote:
Originally Posted by agama
You'll need to populate your array like this:

Code:
typeset -a ARRAY
printf "\tEnter a list of numbers, with spaces: "
read  ans
ARRAY=( $ans )

There are other ways, but I think this is easiest.

You need to set big to something real, not $1. Safe to set to first value of the array.
Code:
BIG=${ARRAY[0]}

You want to look at aray elements, not the indexes:

Code:
for i in ${ARRAY[@]}

Thanks for the explanation! SmilieSmilie Any good book to start Shell Scripting???
 

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
Hash::Merge(3)						User Contributed Perl Documentation					    Hash::Merge(3)

NAME
Hash::Merge - Merges arbitrarily deep hashes into a single hash SYNOPSIS
use Hash::Merge qw( merge ); my %a = ( 'foo' => 1, 'bar' => [ qw( a b e ) ], 'querty' => { 'bob' => 'alice' }, ); my %b = ( 'foo' => 2, 'bar' => [ qw(c d) ], 'querty' => { 'ted' => 'margeret' }, ); my %c = %{ merge( \%a, \%b ) }; Hash::Merge::set_behavior( 'RIGHT_PRECEDENT' ); # This is the same as above Hash::Merge::specify_behavior( { 'SCALAR' => { 'SCALAR' => sub { $_[1] }, 'ARRAY' => sub { [ $_[0], @{$_[1]} ] }, 'HASH' => sub { $_[1] }, }, 'ARRAY => { 'SCALAR' => sub { $_[1] }, 'ARRAY' => sub { [ @{$_[0]}, @{$_[1]} ] }, 'HASH' => sub { $_[1] }, }, 'HASH' => { 'SCALAR' => sub { $_[1] }, 'ARRAY' => sub { [ values %{$_[0]}, @{$_[1]} ] }, 'HASH' => sub { Hash::Merge::_merge_hashes( $_[0], $_[1] ) }, }, }, 'My Behavior', ); # Also there is OO interface. my $merge = Hash::Merge->new( 'LEFT_PRECEDENT' ); my %c = %{ $merge->merge( \%a, \%b ) }; # All behavioral changes (e.g. $merge->set_behavior(...)), called on an object remain specific to that object # The legacy "Global Setting" behavior is respected only when new called as a non-OO function. DESCRIPTION
Hash::Merge merges two arbitrarily deep hashes into a single hash. That is, at any level, it will add non-conflicting key-value pairs from one hash to the other, and follows a set of specific rules when there are key value conflicts (as outlined below). The hash is followed recursively, so that deeply nested hashes that are at the same level will be merged when the parent hashes are merged. Please note that self-referencing hashes, or recursive references, are not handled well by this method. Values in hashes are considered to be either ARRAY references, HASH references, or otherwise are treated as SCALARs. By default, the data passed to the merge function will be cloned using the Clone module; however, if necessary, this behavior can be changed to use as many of the original values as possible. (See "set_clone_behavior"). Because there are a number of possible ways that one may want to merge values when keys are conflicting, Hash::Merge provides several preset methods for your convenience, as well as a way to define you own. These are (currently): Left Precedence This is the default behavior. The values buried in the left hash will never be lost; any values that can be added from the right hash will be attempted. my $merge = Hash::Merge->new(); my $merge = Hash::Merge->new('LEFT_PRECEDENT'); $merge->set_set_behavior('LEFT_PRECEDENT') Hash::Merge::set_set_behavior('LEFT_PRECEDENT') Right Precedence Same as Left Precedence, but with the right hash values never being lost my $merge = Hash::Merge->new('RIGHT_PRECEDENT'); $merge->set_set_behavior('RIGHT_PRECEDENT') Hash::Merge::set_set_behavior('RIGHT_PRECEDENT') Storage Precedence If conflicting keys have two different storage mediums, the 'bigger' medium will win; arrays are preferred over scalars, hashes over either. The other medium will try to be fitted in the other, but if this isn't possible, the data is dropped. my $merge = Hash::Merge->new('STORAGE_PRECEDENT'); $merge->set_set_behavior('STORAGE_PRECEDENT') Hash::Merge::set_set_behavior('STORAGE_PRECEDENT') Retainment Precedence No data will be lost; scalars will be joined with arrays, and scalars and arrays will be 'hashified' to fit them into a hash. my $merge = Hash::Merge->new('RETAINMENT_PRECEDENT'); $merge->set_set_behavior('RETAINMENT_PRECEDENT') Hash::Merge::set_set_behavior('RETAINMENT_PRECEDENT') Specific descriptions of how these work are detailed below. merge ( <hashref>, <hashref> ) Merges two hashes given the rules specified. Returns a reference to the new hash. _hashify( <scalar>|<arrayref> ) -- INTERNAL FUNCTION Returns a reference to a hash created from the scalar or array reference, where, for the scalar value, or each item in the array, there is a key and it's value equal to that specific value. Example, if you pass scalar '3', the hash will be { 3 => 3 }. _merge_hashes( <hashref>, <hashref> ) -- INTERNAL FUNCTION Actually does the key-by-key evaluation of two hashes and returns the new merged hash. Note that this recursively calls "merge". set_clone_behavior( <scalar> ) Sets how the data cloning is handled by Hash::Merge. If this is true, then data will be cloned; if false, then original data will be used whenever possible. By default, cloning is on (set to true). get_clone_behavior( ) Returns the current behavior for data cloning. set_behavior( <scalar> ) Specify which built-in behavior for merging that is desired. The scalar must be one of those given below. get_behavior( ) Returns the behavior that is currently in use by Hash::Merge. specify_behavior( <hashref>, [<name>] ) Specify a custom merge behavior for Hash::Merge. This must be a hashref defined with (at least) 3 keys, SCALAR, ARRAY, and HASH; each of those keys must have another hashref with (at least) the same 3 keys defined. Furthermore, the values in those hashes must be coderefs. These will be called with two arguments, the left and right values for the merge. Your coderef should return either a scalar or an array or hash reference as per your planned behavior. If necessary, use the functions _hashify and _merge_hashes as helper functions for these. For example, if you want to add the left SCALAR to the right ARRAY, you can have your behavior specification include: %spec = ( ...SCALAR => { ARRAY => sub { [ $_[0], @$_[1] ] }, ... } } ); Note that you can import _hashify and _merge_hashes into your program's namespace with the 'custom' tag. BUILT-IN BEHAVIORS Here is the specifics on how the current internal behaviors are called, and what each does. Assume that the left value is given as $a, and the right as $b (these are either scalars or appropriate references) LEFT TYPE RIGHT TYPE LEFT_PRECEDENT RIGHT_PRECEDENT SCALAR SCALAR $a $b SCALAR ARRAY $a ( $a, @$b ) SCALAR HASH $a %$b ARRAY SCALAR ( @$a, $b ) $b ARRAY ARRAY ( @$a, @$b ) ( @$a, @$b ) ARRAY HASH ( @$a, values %$b ) %$b HASH SCALAR %$a $b HASH ARRAY %$a ( values %$a, @$b ) HASH HASH merge( %$a, %$b ) merge( %$a, %$b ) LEFT TYPE RIGHT TYPE STORAGE_PRECEDENT RETAINMENT_PRECEDENT SCALAR SCALAR $a ( $a ,$b ) SCALAR ARRAY ( $a, @$b ) ( $a, @$b ) SCALAR HASH %$b merge( hashify( $a ), %$b ) ARRAY SCALAR ( @$a, $b ) ( @$a, $b ) ARRAY ARRAY ( @$a, @$b ) ( @$a, @$b ) ARRAY HASH %$b merge( hashify( @$a ), %$b ) HASH SCALAR %$a merge( %$a, hashify( $b ) ) HASH ARRAY %$a merge( %$a, hashify( @$b ) ) HASH HASH merge( %$a, %$b ) merge( %$a, %$b ) (*) note that merge calls _merge_hashes, hashify calls _hashify. CAVEATS
This will not handle self-referencing/recursion within hashes well. Plans for a future version include incorporate deep recursion protection. As of Feb 16, 2002, ActiveState Perl's PPM of Clone.pm is only at 0.09. This version does not support the cloning of scalars if passed to the function. This is fixed by 0.10 (and currently, Clone.pm is at 0.13). So while most other users can upgrade their Clone.pm appropriately (and I could put this as a requirement into the Makefile.PL), those using ActiveState would lose out on the ability to use this module. (Clone.pm is not pure perl, so it's not simply a matter of moving the newer file into place). Thus, for the time being, a check is done at the start of loading of this module to see if a newer version of clone is around. Then, all cloning calls have been wrapped in the internal _my_clone function to block any scalar clones if Clone.pm is too old. However, this also prevents the cloning of anything that isn't a hash or array under the same conditions. Once ActiveState updates their Clone, I'll remove this wrapper. AUTHOR
Michael K. Neylon <mneylon-pm@masemware.com> COPYRIGHT
Copyright (c) 2001,2002 Michael K. Neylon. All rights reserved. This library is free software. You can redistribute it and/or modify it under the same terms as Perl itself. perl v5.18.2 2013-11-03 Hash::Merge(3)
All times are GMT -4. The time now is 07:51 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy