Adding array variables together
Hi there! Thanks for taking the time to read this. Anyways, here is my problem.
I have inside a text file both characters and integers and I would like to add the integers together. I used the 'cut' command to extract the integers and assign the array to a variable called 'array'. This is how I did it: Code:
array=$(cut -c52-53 file1) 19 20 21 18 25 30 12 18 25 10 My question is how would I go about adding the integers together? I heard you would use a for loop but I just can't wrap my head around it. Any help is greatly appreciated. FYI: I am programming in BASH |
There are two forms of `for' that you can use.
for num in ${array[@]}; do ... done for (( i=0; i < ${#array[@]}; i++ )); do .. done When working with integers in bash, consider declaring the variable as an integer, and put parenthesis around arithmetic expressions. This can prevent the expression from being taken as a string. e.g. declare -i index=1 echo ${array[($index+1)]} I've found this to be the case when a variable expansion like ${variable:(-8)} |
Quote:
Code:
array = (2 5 7) |
All times are GMT -5. The time now is 06:39 PM. |