Printing array values
I am getting the following printed text. Why is bash complaining printing the array components ?
Code:
Anciator: Code:
|
$i is each item i.e contents of each element of the array not the index.
|
What is the general way to print elements?
With Code:
for i in "${anciator[@]}" Code:
for (( j=0; j<${length}; j++ )); |
Either way.
|
What is the way to print the index when using
Code:
for item in "${anciator[@]}" |
Code:
for i in "${!anciator[@]}"; do |
What would be the equivalent for a counter like so
Code:
for (( j = 0; j < ${#array[@]}; j++ )) |
no, please do not use bash, it is not capable to do advanced handling of anything. It is nto meant to do that.
|
Code:
A=(One Two Three Four) Sorry, I put an i in there instead of a j |
Looping over the array values is most simple if you only want the values.
If you want the indexes and the values then you must loop over the indexes. In bash the normal arrays start with index 0. And normally are contiguous, so the following works. Code:
arr=( a b c d ) Code:
unset arr[1] Code:
for j in ${!arr[@]} And of course "${arr[@]}" generates the existing values. |
Interesting. I don't think that I noticed that before.
Code:
A=(One Two Three Four) |
Quote:
|
How is that the output is just this?
Code:
One |
#10 answers that.
Why don't you run some test yourself. Code:
A=(One Two Three Four five Six Seven Eight) |
Quote:
Code:
A=(One Two Three Four) |
All times are GMT -5. The time now is 06:56 PM. |