3

i have an array of zeros

declare -a MY_ARRAY=( $(for i in {1..100}; do echo 0; done) )

how to set, for example, 12-25th to "1"? i've tried:

MY_ARRAY[12..25]=1
MY_ARRAY[12:25]=1
MY_ARRAY[12-25]=1  

all not working..

the range 12-25 will be variables obtain from another file. I am looking for a simple solution better not involve in looping

please help

1
  • 2
    You must take into account that when declaring an array in the form ( ... ) the index numbering starts in 0, unless you specify the index. e.g. ( [3]="hello" ). Therefore the 12th element in your MY_ARRAY is ${MY_ARRAY[11]}. Commented Jun 24, 2016 at 7:25

3 Answers 3

6

You can use eval here, in this manner:

eval MY_ARRAY[{12..25}]=1\;

If you want to know what is being evaled, replace eval by echo.
Using eval is generally considered as a no-no. But this use of eval here should be completely safe.

On another note,

for i in {1..100}; do echo 0; done

can also be re-written as

printf '%.1s\n' 0{1..100}

EDIT: For start & end being stored in variables, this could work:

$ declare -i start=12
$ declare -i end=12
$ eval $(eval echo "MY_ARRAY[{$start..$end}]=1;")

But in that case, you should really use loops. This answer is only for demonstration/information.

Sign up to request clarification or add additional context in comments.

6 Comments

Wait another min. here, 12..25 are hard-coded, or are obtained from user? If it's the latter, use of eval may be unsafe...
thank you! i dont want to apply for loop in this case, but i didnt know about eval, why is it a no-no?
12..25 are actually variables that obtain from another file, why is it unsafe?
See the link i have given for "Using eval is generally considered as a no-no."...
Then the variables that carry 12 & 25 should be declared with declare -i start; declare -i end Plus, you should know, that brace expansion cannot contain variables. e.g. {$start..$end} will not work.
|
6

Simple one-liner:-

for i in {12..25}; do MY_ARRAY[$i]=1; done

Refer page Arrays for more manipulation examples.

If the start & end values are stored in variables, the brace expansion would not work. In that case, you should use for loop like this:

$ declare -i start=12
$ declare -i end=25
$ for ((i=$start;i<=$end;i++)); do MY_ARRAY[$i]=1; done

1 Comment

@once: As you mentioned that 12 & 25 are numbers stored in a variable, you would need to use loop. This is this answer you would need to use.
2
declare -a MY_ARRAY=(
                      $(printf "%.2s" 0' '{1..11})    # 11 first zeroes
                      $(printf "%.2s" 1' '{12..25})   # 14 ones
                      $(printf "%.2s" 0' '{26..100})  # remaining zeroes
                    )

update
If the values 12 and 25 are in two variables, let's say, From and To:

declare -a MY_ARRAY=(
                       $( eval "{ printf %.2s 0_{1..$((From-1))};
                                  printf %.2s 1_{$From..$To};
                                  printf %.2s 0_{$((To+1))..100}; }" |
                                  tr _ ' '
                        )
                    )

2 Comments

Note that the numbers 12, 25 are obtained from a file; i.e. they are in a variable. Brace expansion will not work with them. but OP has not mentioned it originally in the question. so +1
@anishsane, now I have included another code for that case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.