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` ) Code:
echo ${#array[@]} Code:
number=`echo ${#array[@]}` Code:
array[next_availble_number]=5 Code:
echo ${#array[@] Code:
echo ${array[@] Any ideas? Cheers, MJ |
You could use 'array[$[${#array[@]}+1]]=Whatever'?
|
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 |
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 ) in bash at one go, how do I do the above in one go? Thanks! :) --kvaibhav |
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 ) Hope this helps, MJ |
Array indexes are zero-based. So you don't need to compute anything. Example:
Code:
[yves@localhost ~]$ array=( 1 2 3 4 ) [Edit]@kvaibhav: Code:
[yves@localhost ~]$ for ((i=0; i<${#array[*]}; i++)); do array2[$i]=$((${array[$i]} + 2)); done |
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. 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. |