Problem in summing an array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem in summing an array
# 1  
Old 04-29-2011
Problem in summing an array

Hi Frnds

I need to sum up the values of a single array and store it in another variable, here is what im trying

Code:
     sumArray=0
     for i in ${srvcFeeAmt[@]}
     do
        echo " s of i value----> "$i
        sumArray=`expr $sumArray + ${i}`
     done
     echo "Sum of the array----> "$sumArray

but getting the following error message

Code:
 s of i value----> 20.00
parsingIfNew.ksh[58]: 0: not found [No such file or directory]
 s of i value----> 40.00
parsingIfNew.ksh[58]: +: not found [No such file or directory]
Sum of the array---->

Please help me with this.... thanks in advance.....
# 2  
Old 04-29-2011
Hi,

Shell can't manage floating point so try something like

Code:
sumArray=$(echo "$sumArray + $i" | bc)

# 3  
Old 04-29-2011
MySQL

Thank you so much... Problem resolved!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using awk array problem

I am trying to map values in the input file, where 2nd column depends on the specific value in the 1st column. When 1st column is A place 1 into 2nd column, when it is B, place 2, when C place 3, otherwise no change. My input: U |100|MAIN ST |CLMN1|1 A |200|GREEN LN |CLMN2|2 1 |12... (4 Replies)
Discussion started by: migurus
4 Replies

2. Shell Programming and Scripting

simple array problem

Hello experts, I need help in my code. I have an input file like this: 100814 1205 1724127 7451382 -10 00:30:1b:48:92:3a 100814 1206 1724127 7451382 -72 00:30:1b:48:92:3a 100814 1207 1724127 7451382 -72 00:30:1b:48:90:3b 100814 1208 1724127 7451382 -72 00:30:1b:48:92:3a 100814 1209... (12 Replies)
Discussion started by: enes71
12 Replies

3. Emergency UNIX and Linux Support

Problem with Array in Script

Below is my script. This script is getting an error code such as this one. fileListener.bat: entityArray=craig.uss@pnc.com: not found craig.uss@pnc.com fileListener.bat: entityArray=duns_noncusts.txt: not found duns_noncusts.txt fileListener.bat: entityArray=duns_misc.cpy: not found... (4 Replies)
Discussion started by: mkjp
4 Replies

4. Shell Programming and Scripting

Array problem in Ubuntu

Hi all, I am working in ubuntu for past few weeks .Since I was working in debian I had no problem with arrays.I followed the same method in ubuntu,but is is not working as I expected. Name="apple" Name="orange" print ${Name} Expected result is apple.But I got a error as "Bad... (8 Replies)
Discussion started by: karthigayan
8 Replies

5. Programming

Help on some array problem!!

i have no idea how to make a text file abc efg hij klm nop qrs to be a array such as, arr to be "abc efg" arr "hij kml" etc..... in C (2 Replies)
Discussion started by: tyckelvin1
2 Replies

6. UNIX for Dummies Questions & Answers

Array declaration problem

Hi all, I would like to declare a vector of variables and access them sequentially. Here is my code ARRAY_CT="0001000000 0000100000 0000010000" ELEMENTS_CT=${#ARRAY_CT} echo $ELEMENTS_CT for (( j=1;j<=$ELEMENTS_IS;j++)); do echo ${ARRAY_IS} done ... (2 Replies)
Discussion started by: f_o_555
2 Replies

7. Shell Programming and Scripting

awk array problem

hi i am trying to perform some calculations with awk and arrays. i have this so far: awk 'NR==FNR{ for(i=1; i<=NF; i++) {array+=$i} tot++;next} {for(i=1; i<=NF; i++) {avg=array/tot} {diff=(array - avg)}} {for(i=1; i<=NF; i++) {printf("%5.8f\n",diff)}}' "$count".txt "$count".ttt >... (4 Replies)
Discussion started by: npatwardhan
4 Replies

8. Shell Programming and Scripting

bash array problem

hi friends., i have two files yy.dat and mm.dat containing 110 elements in each if i read them into variables it is just showing only 4 elements instead of 110 elements My script is like this ################################## /bin/bash declare -a yy=(`cat yy.dat`) echo "No of values in... (1 Reply)
Discussion started by: yagnesh
1 Replies

9. Shell Programming and Scripting

Array problem

I am using /bin/ksh for this problem. I have created some arrays with variable names as the array names: cnt=1 { while read myline; do tempmeas="${meas%%;*}" cto="${meas#*;}" tempstream=$stream # wholemeas holds the name of the array # each array name... (0 Replies)
Discussion started by: ajgwin
0 Replies

10. Shell Programming and Scripting

array problem

Dear Experts, please help me out once again my array concepts is not very clear i have one text file like. 1|usa|hh 2|usa|ll 3|usa|vg 4|uk|nn 5|uk|bb 6|kuwait|mm 6|kuwait|jkj 7|dubai|hh i want to store the third fied of a text file in he array and after that it should give me some... (6 Replies)
Discussion started by: shary
6 Replies
Login or Register to Ask a Question