LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 11-21-2011, 12:19 PM   #1
olof_
LQ Newbie
 
Registered: Dec 2010
Location: Newcastle upon Tyne
Distribution: Ubuntu Linux
Posts: 3

Rep: Reputation: 0
addition using array values into a variable not possible?


Hi there.

I am trying to add every value in an array together in a single variable. The script is working, but throws an error:

syntax error: operand expected (error token is "+")
Is it not possible to add values from an array into a non-array variable?

Code:
echo Input some numbers, separated by a space please:
read -a choice

amount=${#choice[@]}

for a in `seq 0 $amount`; do
sum=$(($sum+${choice[$a]}))
done

echo The result after adding all numbers is: $sum
Thanks in advance.
 
Old 11-21-2011, 12:36 PM   #2
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 640

Rep: Reputation: 375Reputation: 375Reputation: 375Reputation: 375
Hi.
This should work:
Code:
echo Input some numbers, separated by a space please:
read -a choice

amount=${#choice[@]}

for a in `seq 0 $amount`; do
sum=$((sum + choice[$a]))
done

echo The result after adding all numbers is: $sum
 
1 members found this post helpful.
Old 11-21-2011, 12:52 PM   #3
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,698

Rep: Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988
There is some more overhead that is not really required:
Code:
#!/bin/bash

echo Input some numbers, separated by a space please:
read -a choice

for a in ${!choice[*]}
do
    ((sum+=choice[a]))
done

echo The result after adding all numbers is: $sum
 
1 members found this post helpful.
Old 11-21-2011, 03:44 PM   #4
olof_
LQ Newbie
 
Registered: Dec 2010
Location: Newcastle upon Tyne
Distribution: Ubuntu Linux
Posts: 3

Original Poster
Rep: Reputation: 0
Thank you very much!
Really impressively quick answers.
 
Old 11-21-2011, 05:00 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
Or how about this?

Code:
$ choice=( 1 2 3 4 5 )
$ IFS=+

$ echo $(( ${choice[*]} ))
15
It may not work correctly with negative numbers though.
 
  


Reply

Tags
array, bash


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[bash] indirect array reference to array with values containing spaces Meson Linux - Software 9 06-04-2010 10:38 PM
Storing multiple options in a single variable (binary addition?) matt_heys Programming 1 04-03-2009 04:38 PM
Bash Variable Array, Trying to add another value into the array helptonewbie Linux - Newbie 6 03-03-2009 12:18 AM
Values like VARIABLE[i] ? BlueSpirit Programming 5 04-27-2007 02:35 PM
extracting values from an array bahadur Programming 2 08-14-2006 02:07 AM


All times are GMT -5. The time now is 10:43 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration