ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
test=(1 2 3 4 5)
for car in ${test[@]}
do
echo "Element : $car"
if [ $car -eq 1 ]
then
test=( "${test[@]" 6 )
fi
done
The idea of above code is that if variable $car equals 1, new element is added "6" to an array. But i don't know why when i am printing
all elements of this array (echo "Element : $car") this element ("6") is not mentioned, but if i make a command which check an amount of contained elements by array it will be 6 elements.
I was wondering about using while command but it is not easy.
Do you have an idea how use this element ("6") which was added before.
Click here to see the post LQ members have rated as the most helpful post in this thread.
test=(1 2 3 4 5); x=0
while [ "${test[$x]}" ]; do
car="${test[$x]}"; echo "Element : $car"
if [ $car -eq 1 ]; then
test=( "${test[@]}" 6 )
fi
x=$((x+1)); done
I believe the problem in your original code was that the for construct was grabbing all elements of the array only once, at the start, and running the loop once for each element; and the fact that an element was added to the array at some later point did not change the number of loops that are done - put simply, you correctly determined that the array was not being checked dynamically to see if there were any more elements that the current one.
The code I show above, using a while loop, seems to do what you wish it to do. Here's a sample output, first with no "1" in the array, and then, with a "1" in the array:
Code:
sasha@reactor: test=(8 2 3 4 5); x=0; while [ "${test[$x]}" ]; do car="${test[$x]}"; echo "Element : $car"; if [ $car -eq 1 ]; then test=( "${test[@]}" 7 ); fi; x=$((x+1)); done
Element : 8
Element : 2
Element : 3
Element : 4
Element : 5
sasha@reactor: test=(1 2 3 4 5); x=0; while [ "${test[$x]}" ]; do car="${test[$x]}"; echo "Element : $car"; if [ $car -eq 1 ]; then test=( "${test[@]}" 6 ); fi; x=$((x+1)); done
Element : 1
Element : 2
Element : 3
Element : 4
Element : 5
Element : 6
sasha@reactor:
Ahh konsolebox, thanks! Early in the morning here, I overlooked using # to get the number of elements in the array, instead, assuming (bad!) that # was only useful for determining the length of a string variable. Nice one! Your solution is more like what I would have posted, had I not been asleep at the wheel
Ahh konsolebox, thanks! Early in the morning here, I overlooked using # to get the number of elements in the array, instead, assuming (bad!) that # was only useful for determining the length of a string variable. Nice one! Your solution is more like what I would have posted, had I not been asleep at the wheel
When somebody makes a funny quote about wheels it always reminds me about the description that a zodiac book described me.. a free wheeler. Not that I really believe in it but somehow I find the description a bit funny and recalling it always makes me smile a bit.
Good day kurumi. Does the expanded indices in ${!test[@]} also gets updated when test[] gets a new appended value?
Code:
test=(1 2 3 4 5)
echo "before: ${!test[@]}"
for i in ${!test[@]}
do
if [[ "${test[$i]}" = 1 ]] ;then
test=(${test[@]} "6")
fi
done
echo ${test[@]}
echo "after: ${!test[@]}"
Hello kurumi. The method's also quite valid however the changes should immediately be felt when inside the loop and that number 6 will also be tested if it is equal to 1.
there are two 1's, so there are two 6's. the for loop will not be iterating the new values because in the first place, its only iterating the initial elements.
its a bit strange to me, why there is need to test for "6" against 1, since 6 is only added when the element is 1. ie, why do we need to test what's being added, since we know it couldn't be 1. ?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.