LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash Variable Array, Trying to add another value into the array (https://www.linuxquestions.org/questions/linux-newbie-8/bash-variable-array-trying-to-add-another-value-into-the-array-692226/)

helptonewbie 12-22-2008 08:00 AM

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

unSpawn 12-22-2008 04:53 PM

You could use 'array[$[${#array[@]}+1]]=Whatever'?

helptonewbie 12-22-2008 05:06 PM

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

kvaibhav 03-02-2009 07:33 AM

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

helptonewbie 03-02-2009 07:50 AM

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

theYinYeti 03-02-2009 07:55 AM

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

kvaibhav 03-02-2009 11:18 PM

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


All times are GMT -5. The time now is 07:22 AM.