How do I find the sum of values from two arrays?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How do I find the sum of values from two arrays?
# 1  
Old 12-04-2011
How do I find the sum of values from two arrays?

Hi

I have redc[] containing the values 3, 6, 2, 8, and 1.

I have work[] containing the values 8, 2, 11, 7, and 9.

Is there a way to find the sum of redc and work?

I need to compare the sum of those two arrays to something else, so is it okay to put that into my END?

TY!
# 2  
Old 12-04-2011
Seems a few of us answered this question already:

https://www.unix.com/shell-programmin...sum-array.html

Yes, you can put it in the END block of code.
# 3  
Old 12-04-2011
Code:
perl -e '@x=(3,6,2,8,1);@y=(8,2,11,7,9);for($i=0;$i<=$#x;$i++){$sum+=($x[$i]+$y[$i])};print $sum'

This User Gave Thanks to balajesuri For This Post:
# 4  
Old 12-05-2011
try it once and let me know ur feedback ..

I hope it will help you to solve the summation part and I believe comparison part you can do it yourself :-)
Code:
redc=(3 6 2 8 1)
work=(8 2 11 7 9)

function sum {
        local sm=0
        local tmp
        tmp=(`echo "$@"`)

        for i in ${tmp[*]}
        do
                sm=$[ $sm + $i ];
        done
echo $sm
}

echo "The Sum of redc is :: " ; sum ${redc[*]}
echo "The Sum of work is :: " ; sum ${work[*]}


Last edited by Franklin52; 12-05-2011 at 09:42 AM.. Reason: Please use code tags for data and code samples, thank you
# 5  
Old 12-05-2011
You can do it without temp arrays - use argument and shift it.
Code:
redc=(3 6 2 8 1)
work=(8 2 11 7 9)

sum()
{
        sm=0
        while [ $# -gt 0 ]
        do
                val=$1
                shift
                ((sm+=val))
        done
        echo $sm
}

echo "The Sum of redc is :: $(sum ${redc[*]} ) "
echo "The Sum of work is :: $(sum ${work[*]} ) "

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk sum of 2 arrays and compare

i'm new to awk, and i've been searching on the forum for sum of a column but all the scripts does sum a column of an entire file. I've a file like this: cat file.txt 1234 5678 5678 1234 I want to use awk to do sum of each column per line not entire file, compare the two then write the... (1 Reply)
Discussion started by: chofred
1 Replies

2. Shell Programming and Scripting

Sum elements of 2 arrays excluding labels

I'm looking for an efficient way to sum elements from 2 arrays using AWK and preserve header as well as sample names in the output array. I have Ubuntu 16.04 LTS. For example; ARRAY 1 SAMPLE DERIVED ANCESTRAL Sample1 14352 0 Sample2 14352 0 Sample3 14352 0 Sample4 ... (8 Replies)
Discussion started by: Geneanalyst
8 Replies

3. UNIX for Dummies Questions & Answers

Sum up values followed by pattern

I have a file with data merged from multiple files. File contains header, data and trailer of multiple files. The trailer starts with 99 and delimiter is ~. Trailer 99~120 99~30 As it is a merged file we i have multiple lines followed by 99~. Need help to find sum of values which are there... (4 Replies)
Discussion started by: santoshdrkr
4 Replies

4. Shell Programming and Scripting

How to find sum of any 'n' number of values from file matching target value?

I have a simple text file having payment amount value on each line. At the end of day 'n' number of payments created difference in amount that I need to match from this file. I have information about how many payments created difference and difference amount. Please help me to build shell... (3 Replies)
Discussion started by: swats007
3 Replies

5. UNIX for Dummies Questions & Answers

sum values based on ID

Hi, I would like to be able to sum up the counts of a column by the ID of another column. Example (although the actual file I have has thousands of IDs): Input file: A1BG-AS1:001 3 A1BG-AS1:002 0 A1BG-AS1:003 2 A1CF:001 1038 A1CF:002 105 A1CF:003 115 A1CF:004 137 Desired output... (3 Replies)
Discussion started by: fadista
3 Replies

6. Shell Programming and Scripting

arrays and two values

I have requirement where I need to capture the highest values of items from a feed that runs for N hours. For example lets assume my data looks like this first feed ======== appples 10 oranges 20 pears 14 second feed ========== apples 5 oranges 30 pears 1 Last feed... (6 Replies)
Discussion started by: BeefStu
6 Replies

7. Shell Programming and Scripting

Storing values in arrays

I have the following csh script which lets the use pass the following as an argument -legend=tag1/tag2/tag3/tag4/tag5/tag6/tag7 We pass a number of tags separated by '/'. I want to save the legend tags in an array and have done as below. How can I improve on this as things are getting quite... (3 Replies)
Discussion started by: kristinu
3 Replies

8. Shell Programming and Scripting

How to sum up two decimal values?

I am running the following script : cat ind_sls_extr_UX.out_sorted | while read each_rec do count=`echo "${each_rec}" | cut -c1-2` if then final_amount=0 amount=`echo "${each_rec}" | cut -c280-287` echo "${amount}" final_amount=`expr ${amount} + ${amount}` ... (7 Replies)
Discussion started by: mady135
7 Replies

9. Shell Programming and Scripting

How to sum values from top

Hi. Im looking for way to sum numbers from top. For example i have such command top -b -n | grep Cpu | cut -c 35 - 39 which give me output 97.0 . Ho can i do with that value any arithmetic actions (for example 97.0 +1)? Using c = $((top -b -n | grep Cpu | cut -c 35 - 39)) gives me... (8 Replies)
Discussion started by: qdf
8 Replies

10. Shell Programming and Scripting

How to sum column 1 values

I have a file file like this. I want to sum all column 1 values. input A 2 A 3 A 4 B 4 B 2 Out put A 9 B 6 (3 Replies)
Discussion started by: suresh3566
3 Replies
Login or Register to Ask a Question