PDA

View Full Version : Accessing array of integer



colau
August 16th, 2009, 05:12 AM
ar="6 4 6 1 5"
for (( i = 0; i <= 4; i++ ))
do
echo ar[$i]
done

How can i print:


6
4
6
1
5

kaibob
August 16th, 2009, 05:41 AM
I'm new to arrays, so this may be a dumb question, but have you actually created an array? From what I've learned so far, it should be:


array=( 6 4 6 1 5 ) ; for item in ${array[@]} ; do echo $item ; done

or


array=( 6 4 6 1 5 ) ; for (( i = 0; i <= 4; i++ )) ; do echo ${array[$i]} ; done

colau
August 16th, 2009, 05:46 AM
I'm new to arrays, so this may be a dumb question, but have you actually created an array? From what I've learned so far, it should be:


array=( 6 4 6 1 5 ) ; for item in ${array[@]} ; do echo $item ; done

Then, getting say the first element of the array would be:


array=( 6 4 6 1 5 ) ; for item in ${array[0]} ; do echo $item ; done
Thank you.
Trying to solve with this pattern:


for (( i = 0; i <= 4; i++ ))

kaibob
August 16th, 2009, 05:58 AM
Thank you.
Trying to solve with this pattern:


for (( i = 0; i <= 4; i++ ))


Sorry, like I said, I'm new to all this. I'm sure geirha, stroyan, ghostdog74 or one of the other very knowledgeable forum members will have an answer to your question.

soltanis
August 16th, 2009, 06:19 AM
I am not familiar with this syntax. What language is this being done with?

MadCow108
August 16th, 2009, 09:39 AM
looks like bash (at least the for loop)
in that case you can to do it like that:


ar=(6 4 6 1 5);
for (( i = 0; i <= 4; i++ ));
do
echo ${ar[$i]}
done

nvteighen
August 16th, 2009, 11:50 AM
I am not familiar with this syntax. What language is this being done with?

Aj... Again, people have to please put the language they're asking about in the thread title... Heck, specially in shell scripting languages, which are very close to eachother in syntax but very different in semantics.

colau
August 16th, 2009, 12:16 PM
looks like bash (at least the for loop)
in that case you can to do it like that:


ar=(6 4 6 1 5);
for (( i = 0; i <= 4; i++ ));
do
echo ${ar[$i]}
done

Thank you.
Can you tell in echo ${ar[$i]}, why is the color of first brace different from the second in gedit?

MadCow108
August 16th, 2009, 12:19 PM
this just seems to be a bug in syntax highlighting of gedit
it probably "thinks" the first brace is part of the variable name so it colours it wrong

colau
August 16th, 2009, 12:49 PM
this just seems to be a bug in syntax highlighting of gedit
it probably "thinks" the first brace is part of the variable name so it colours it wrong
Which version of gedit are you using?

colau
August 18th, 2009, 04:02 AM
array=( 6 4 6 1 5 ) ; for item in ${array[@]} ; do echo $item ; done

Is there any other option except ${array[@]}?
What does it mean by @?
It starts to print from array[0].
I do not want to use for ((i=1;i<=5;i++)) pattern to print the element of array.
Is it possible to print from arr[1] using this pattern for vv in



#!/bin/bash

arr="0"
cnt=1
for i in $(seq 1 5)
do
m=`expr $i % 2`
if [ $m -ne 0 ];then
arr[$cnt]=$i
cnt=`expr $cnt + 1`
fi
done

for vv in ${arr[@]}
do
echo $vv
done

geirha
August 18th, 2009, 09:48 AM
#!/bin/bash

arr=(0)
for i in {1..5}; do
(( i%2 == 0)) && arr+=($i)
# You can also do ((i%2)) || arr+=($1)
done

for vv in "${arr[@]}"; do
echo "$vv"
done


Make sure to quote the expansion "${arr[@]}". See also http://mywiki.wooledge.org/BashFAQ/005

EDIT: Oh and to skip element 0 (i.e. start from element 1), you can use "${arr[@]:1}"

colau
August 18th, 2009, 12:02 PM
#!/bin/bash

arr=(0)
for i in {1..5}; do
(( i%2 == 0)) && arr+=($i)
# You can also do ((i%2)) || arr+=($1)
done

for vv in "${arr[@]}"; do
echo "$vv"
done


Make sure to quote the expansion "${arr[@]}". See also http://mywiki.wooledge.org/BashFAQ/005

EDIT: Oh and to skip element 0 (i.e. start from element 1), you can use "${arr[@]:1}"

Thank you.

colau
August 18th, 2009, 02:47 PM
Make sure to quote the expansion "${arr[@]}".

If I remove double quote from "${arr[@]}" it works too.

geirha
August 18th, 2009, 07:27 PM
If I remove double quote from "${arr[@]}" it works too.

In your case when it only contains integers, it will work without quotes, however, if any of the elements contained spaces or glob characters, you'd get undesired results. You might as well make it a good habit right away.