LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-24-2010, 05:38 AM   #1
Garcia16
LQ Newbie
 
Registered: Sep 2010
Posts: 2

Rep: Reputation: 0
Bash - read array dynamically


Hi,

there is some code :

Code:
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.
Old 09-24-2010, 06:11 AM   #2
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
Code:
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:
DOes that help?
 
2 members found this post helpful.
Old 09-24-2010, 06:31 AM   #3
Garcia16
LQ Newbie
 
Registered: Sep 2010
Posts: 2

Original Poster
Rep: Reputation: 0
Hi,

splendid code with examples of using Thank you very much.
 
Old 09-24-2010, 06:33 AM   #4
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Hi Garcia16 and GrapefruiTgirl. Maybe this could help as well:
Code:
test=(1 2 3 4 5)
for (( i = 0; i < ${#test[@]}; ++i )); do
    echo "Element : $car"
    if [[ $car -eq 1 ]]; then
        test=("${test[@]" 6)
    fi
done
 
1 members found this post helpful.
Old 09-24-2010, 06:39 AM   #5
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
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
 
1 members found this post helpful.
Old 09-24-2010, 06:59 AM   #6
kurumi
Member
 
Registered: Apr 2010
Posts: 228

Rep: Reputation: 53
Code:
for i in ${!test[@]}
do
  if [ ${test[$i]} -eq 1 ] ;then
      .....
  fi
done
 
Old 09-24-2010, 07:03 AM   #7
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Quote:
Originally Posted by kurumi View Post
Code:
for i in ${!test[@]}
do
  if [ ${test[$i]} -eq 1 ] ;then
      .....
  fi
done
Good day kurumi. Does the expanded indices in ${!test[@]} also gets updated when test[] gets a new appended value?
 
Old 09-24-2010, 07:19 AM   #8
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Quote:
Originally Posted by GrapefruiTgirl View Post
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.
 
Old 09-24-2010, 07:24 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
So here is another take:
Code:
#!/bin/bash

test=(1 2 3 4 5)

echo ${test[@]}

for car in ${test[*]}
do
    echo "Element : $car"
    (( car == 1 )) && test+=(6)
done

echo ${test[@]}
Edit: Appears I got caught not doing what Grapefruitgirl said
This is the same plan as hers:
Code:
#!/bin/bash

test=(1 2 3 4 5)

echo ${test[@]}

x=0
while [[ ${test[x]} ]]
do
    car=${test[x++]}
    echo "Element : $car"
    (( car == 1 )) && test+=(6)
done

echo ${test[@]}

Last edited by grail; 09-24-2010 at 07:33 AM.
 
1 members found this post helpful.
Old 09-24-2010, 08:49 AM   #10
kurumi
Member
 
Registered: Apr 2010
Posts: 228

Rep: Reputation: 53
Quote:
Originally Posted by konsolebox View Post
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[@]}"
 
Old 09-24-2010, 10:19 PM   #11
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
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.
 
Old 09-24-2010, 10:28 PM   #12
kurumi
Member
 
Registered: Apr 2010
Posts: 228

Rep: Reputation: 53
Quote:
Originally Posted by konsolebox View Post
changes should immediately be felt when inside the loop and that number 6 will also be tested if it is equal to 1.
but why should the new value be tested? the original requirement is to append new text to array if element is 1. If you run my script using

Code:
test=(1 2 3 4 5 1)
Code:
$ bash myscript.sh
before: 0 1 2 3 4 5
1 2 3 4 5 1 6 6
after: 0 1 2 3 4 5 6 7
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.

Last edited by kurumi; 09-24-2010 at 10:33 PM.
 
Old 09-25-2010, 12:32 AM   #13
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Quote:
Originally Posted by kurumi
but why should the new value be tested?
Well I am guessing that is what the OP meant by:
Quote:
Originally Posted by Garcia16
read array dynamically
 
Old 09-25-2010, 12:54 AM   #14
kurumi
Member
 
Registered: Apr 2010
Posts: 228

Rep: Reputation: 53
Quote:
Originally Posted by grail View Post
Well I am guessing that is what the OP meant by:
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. ?
 
Old 09-25-2010, 01:36 AM   #15
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
At first I also thought that the code was just a shell scripting trial or game.
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
local dynamically allocated array in fortran otoomet Programming 5 10-21-2009 08:28 AM
Bash script, read file into array (Newbie) TheMeeks Programming 6 04-17-2007 08:20 AM
Dynamically set array length in Fortran77? halfpower Programming 2 12-15-2005 08:03 AM
In C, using qsort for dynamically allocated array ntmsz Programming 7 08-23-2005 10:33 AM
dynamically expanding an array in c++ a1ghagh05t Programming 4 03-07-2004 09:00 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:26 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration