LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-25-2009, 04:39 PM   #1
nobody123
LQ Newbie
 
Registered: Feb 2009
Location: Toronto, ON
Distribution: Fedora 10
Posts: 16

Rep: Reputation: 0
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)
echo $array
And the output of this is:
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
 
Old 02-25-2009, 05:30 PM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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)}

Last edited by jschiwal; 02-25-2009 at 05:40 PM.
 
Old 02-26-2009, 04:43 PM   #3
nobody123
LQ Newbie
 
Registered: Feb 2009
Location: Toronto, ON
Distribution: Fedora 10
Posts: 16

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by jschiwal View Post
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)}
Thanks for helping me again. I don't really know how to implement the code though. What I really am looking for was just adding the values in an array. Here's an example:

Code:
array = (2 5 7)
I was just wondering how to add the values of 2, 5, and 7 to make 14. Maybe I'm implementing your code wrong?
 
  


Reply


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
array variables from html form not processed hil4d Linux - Server 2 04-20-2008 10:11 PM
adding variables to the PATH myrto Linux - Newbie 8 01-23-2007 08:34 PM
MD5.pm - adding to the @INC array nitrohuffer2001 Programming 1 12-31-2006 10:10 PM
BASH - Array Variables Micro420 Programming 5 12-15-2006 05:49 PM
adding $PATH variables karupt Linux - Newbie 5 03-07-2004 06:27 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:34 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
Open Source Consulting | Domain Registration