Skip to main content
edited tags
Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k
added 202 characters in body
Source Link
Tim
  • 106.7k
  • 234
  • 650
  • 1.1k

I have an array, and I would like to repeat each element except the first and last elements.

For example if the array has threefive elements 1 2 3 4 5, then after repeating, its elements should be 1 2 2 3 3 4 4 5.

My bash commands are

$ newarr=("${myarr[0]}")
$ for i in $(seq 1 $((${#myarr[@]}-2))) ; do newarr+=( "${myarr[i]}" "${myarr[i]}"); done
$ newarr+=("${myarr[-1]}")

Is there a more clear way than mine?

I am also wondering about wrappinghow to wrap that into a function which takes myarr as argument, and returnreturns newarr. (After creating such a function, I will read an array from a file, so that each element stores a line in the file, and then call the function on the array. If creating such a function is not a good approach, let me know.)

Thanks.

I have an array, and I would like to repeat each element except the first and last elements.

For example if the array has three elements 1 2 3 4 5, then after repeating, its elements should be 1 2 2 3 3 4 4 5.

My bash commands are

$ newarr=("${myarr[0]}")
$ for i in $(seq 1 $((${#myarr[@]}-2))) ; do newarr+=( "${myarr[i]}" "${myarr[i]}"); done
$ newarr+=("${myarr[-1]}")

Is there a more clear way than mine?

I am also wondering about wrapping that into a function which takes myarr as argument, and return newarr.

Thanks.

I have an array, and I would like to repeat each element except the first and last elements.

For example if the array has five elements 1 2 3 4 5, then after repeating, its elements should be 1 2 2 3 3 4 4 5.

My bash commands are

$ newarr=("${myarr[0]}")
$ for i in $(seq 1 $((${#myarr[@]}-2))) ; do newarr+=( "${myarr[i]}" "${myarr[i]}"); done
$ newarr+=("${myarr[-1]}")

Is there a more clear way than mine?

I am also wondering about how to wrap that into a function which takes myarr as argument, and returns newarr. (After creating such a function, I will read an array from a file, so that each element stores a line in the file, and then call the function on the array. If creating such a function is not a good approach, let me know.)

Thanks.

deleted 144 characters in body
Source Link
Tim
  • 106.7k
  • 234
  • 650
  • 1.1k

I have an array, and I would like to repeat each element except the first and last elements.

For example if the array has three elements 1 2 3 4 5, then after repeating, its elements should be 1 2 2 3 3 4 4 5.

My bash commands are

$ len=${#myarr[@]}
$ indices=($(seq 1 $((len-2))))
$ newarr=("${myarr[0]}")
$ for i in $(seq 1 $((${indices[@]#myarr[@]}-2))) ; do newarr+=( "${myarr[i]}" "${myarr[i]}"); done
$ newarr+=($"${myarr[-1]}")

How canIs there a more clear way than mine?

I eliminate creatingam also wondering about wrapping that into a function which takes lenmyarr as argument, and return indicesnewarr, and move their definitions directly to the loop?.

Is there a more clear way than mine? Thanks.

I have an array, and I would like to repeat each element except the first and last elements.

For example if the array has three elements 1 2 3, then after repeating, its elements should be 1 2 2 3.

My bash commands are

$ len=${#myarr[@]}
$ indices=($(seq 1 $((len-2))))
$ newarr=("${myarr[0]}")
$ for i in ${indices[@]}; do newarr+=( "${myarr[i]}" "${myarr[i]}"); done
$ newarr+=(${myarr[-1]})

How can I eliminate creating len and indices, and move their definitions directly to the loop?

Is there a more clear way than mine? Thanks.

I have an array, and I would like to repeat each element except the first and last elements.

For example if the array has three elements 1 2 3 4 5, then after repeating, its elements should be 1 2 2 3 3 4 4 5.

My bash commands are

$ newarr=("${myarr[0]}")
$ for i in $(seq 1 $((${#myarr[@]}-2))) ; do newarr+=( "${myarr[i]}" "${myarr[i]}"); done
$ newarr+=("${myarr[-1]}")

Is there a more clear way than mine?

I am also wondering about wrapping that into a function which takes myarr as argument, and return newarr.

Thanks.

Source Link
Tim
  • 106.7k
  • 234
  • 650
  • 1.1k
Loading