LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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
 
LinkBack Search this Thread
Old 12-22-2008, 08:00 AM   #1
helptonewbie
Member
 
Registered: Aug 2006
Location: England Somewhere
Distribution: Mandriva, PCLinuxOS, Karoshi, Suse, Redhat, Ubuntu
Posts: 517

Rep: Reputation: 39
Question Bash Variable Array, Trying to add another value into the array


Hi All,

Just thinking about a variable array and i'd like to if possible...
when working with an array simply add a value to the array at the next available slot/number so to speak.

IE
i have an array:-
Code:
array=( `echo 1 2 3 4` )
this obviously giving me 4 values in my array
Code:
echo ${#array[@]}
4
Instead of then having to do something like:-
Code:
number=`echo ${#array[@]}`
((number++)) #add one
array[${number}]=5
Is it possible to not have to know the next number in the array so the value can just be added to the next available slot in the array so to speak. ie

Code:
array[next_availble_number]=5
rather than going through the rigmarole of all that above. It would be much nicer if its possible to just say chuck this value anywhere in the array as i don't care where it goes. Just as long as its not overwriting something already in the array, and is using the next available number in the array so that when you do something like:-
Code:
echo ${#array[@]
or

Code:
echo ${array[@]
you still get returned the correct numbers of 5 and 1 2 3 4 5

Any ideas?

Cheers,
MJ
 
Old 12-22-2008, 04:53 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 26,541
Blog Entries: 51

Rep: Reputation: 2604Reputation: 2604Reputation: 2604Reputation: 2604Reputation: 2604Reputation: 2604Reputation: 2604Reputation: 2604Reputation: 2604Reputation: 2604Reputation: 2604
You could use 'array[$[${#array[@]}+1]]=Whatever'?
 
Old 12-22-2008, 05:06 PM   #3
helptonewbie
Member
 
Registered: Aug 2006
Location: England Somewhere
Distribution: Mandriva, PCLinuxOS, Karoshi, Suse, Redhat, Ubuntu
Posts: 517

Original Poster
Rep: Reputation: 39
Hey unspawn,
Thanks very much, looks perfect. I think i need to look into more details with bash and all the [] {} () (()) and how they all work.

Thanks though, i can see that should work fine.

Cheers,
MJ
 
Old 03-02-2009, 07:33 AM   #4
kvaibhav
LQ Newbie
 
Registered: Dec 2005
Posts: 4

Rep: Reputation: 0
Question

Dear all,
is it possible to add a constant integer to all array elements
in one go.

e.g.

I have and array:

Code:
myarray=( 2 3 2 14 6 32 5 455 23 )

On completing the operation, I want the array to be modified as:

Code:
myarray=( 4 5 4 16 8 34 7 457 25 )
Just as one is able to substitute all strings in an array
in bash at one go, how do I do the above in one go?

Thanks!


--kvaibhav
 
Old 03-02-2009, 07:50 AM   #5
helptonewbie
Member
 
Registered: Aug 2006
Location: England Somewhere
Distribution: Mandriva, PCLinuxOS, Karoshi, Suse, Redhat, Ubuntu
Posts: 517

Original Poster
Rep: Reputation: 39
Hello There,
Not sure if this is the best way but a quick go at it from my point of view and i come up with:-

Code:
# myarray=( 4 5 4 16 8 34 7 457 25 )
# count=0
# for i in ${myarray[@]}; do myarray[$count]=`expr $i + 2`; ((count++));done
# echo ${myarray[@]}
6 7 6 18 10 36 9 459 27
Just change the "2" in the `expr $i + 2` to be what ever amount you want to be added to each number in the variable.

Hope this helps,
MJ

Last edited by helptonewbie; 03-02-2009 at 07:52 AM.
 
Old 03-02-2009, 07:55 AM   #6
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 61
Array indexes are zero-based. So you don't need to compute anything. Example:
Code:
[yves@localhost ~]$ array=( 1 2 3 4 )
[yves@localhost ~]$ echo ${#array[*]}
4
[yves@localhost ~]$ array[${#array[*]}]=5
[yves@localhost ~]$ array[${#array[*]}]=6
[yves@localhost ~]$ for ((i=0; i<${#array[*]}; i++)); do echo "array[$i] = ${array[$i]}"; done
array[0] = 1
array[1] = 2
array[2] = 3
array[3] = 4
array[4] = 5
array[5] = 6
Yves.

[Edit]@kvaibhav:
Code:
[yves@localhost ~]$ for ((i=0; i<${#array[*]}; i++)); do array2[$i]=$((${array[$i]} + 2)); done

Last edited by theYinYeti; 03-02-2009 at 08:06 AM.
 
Old 03-02-2009, 11:18 PM   #7
kvaibhav
LQ Newbie
 
Registered: Dec 2005
Posts: 4

Rep: Reputation: 0
Smile

Dear all,
thanks for the reply.

The solution IS evident and thanks for it.


What I was looking for, was an 'array operation' that does the same.

For example, in the following:

Code:
# Replace all occurrences of substring.
echo ${arrayZ[@]//iv/YY}    # one two three four fYYe fYYe
                            # Applied to all elements of the array.
(Ref: http://tldp.org/LDP/abs/html/arrays.html)

all elements of array get altered on just one array operation.

I was looking for such a solution.

Thanks!



--kvaibhav
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Confused: how to use variable as the name of an array in perl littletransformer Programming 18 03-13-2008 09:08 PM
About the array variable sean_zhang Linux - Newbie 3 02-27-2008 12:01 AM
Bash indirect reference to array variable(s) sixerjman Programming 6 10-25-2006 11:18 AM
Bash Local Variable Recursion With Array jshivers Programming 0 06-16-2006 04:31 PM
Variable Array in C mojozoox Programming 6 12-16-2003 01:39 AM


All times are GMT -5. The time now is 01:08 AM.

Main Menu
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