简体   繁体   English

删除数组bash中的元素

[英]delete elements in array bash

im trying to delete two elements at same time in my array in bash script my code is 我试图在bash脚本中同时删除数组中的两个元素,我的代码是

elegidos = (1 2 3 4 5 6 7)
i=0
j=${#elegidos[@]}
delete=($i $j)
while [ $i -le $j ]; do
        #elegidosmenosdos=${elegidos[@]/$i:$j}
        echo ${elegidos[@]/$delete}
        delete=($i $j)
        let "i++"
        let "j--"

done

the output that i have is 我的输出是

1 2 3 4 5 6 7 1 2 3 4 5 6 7

1 2 3 4 5 6 7 1 2 3 4 5 6 7

2 3 4 5 6 7 2 3 4 5 6 7

1 3 4 5 6 7 1 3 4 5 6 7

and i need 21 different combinations with five elements using seven numbers output example 我需要使用七个数字输出示例的五个元素有21种不同的组合

1 2 3 4 5 1 2 3 4 5

2 3 4 5 6 2 3 4 5 6

7 6 5 4 3 . 7 6 5 4 3。 . . . .(21 MORE) (更多21个)

Well, it took a minute to suss out that you were simply wanting to do an end-to-middle delete of two-elements at a time working from the ends of the array, deleting the end nodes at the same time, increment/decrement your counters and repeat until you reached the middle. 好了,花了一分钟,苏斯的是,你只是想要做一个终端到中间一次删除从阵列的两端工作的两个元素,同时删除终端节点,递增/递减重复您的计数器,直到到达中间位置。 Why you want to do it this way is a bit of a mystery. 为什么要以这种方式进行操作有点神秘。 You can, of course, simply unset elegidos and unset all values at once. 当然,您可以简单地unset elegidos并立即取消设置所有值。 However, if you want to work in from both ends -- that fine too if you have a purpose for it. 但是,如果您想从两端进行工作,那么如果您有目标也可以。

You have several problems in your script. 您的脚本中有几个问题。 In bash, all arrays are zero indexed . 在bash中,所有数组的索引为零 Your array has 7-elements, so the valid indexes are 0-6 . 您的数组有7个元素,因此有效索引为0-6 Therefore, you j was wrong to begin with. 因此,您j开头是错误的。 You needed to subtract 1 from the number of elements to get the index for the end-element, eg 您需要从元素数中减去1来获取末元素的索引,例如

i=0
j=$((${#elegidos[@]} - 1))

bash provides a C-style loop that can greatly simplify your task. bash提供了一个C风格的循环,可以大大简化您的任务。 While you are free to use a while a C-style loop can handle the index increment and decrement seamlessly, eg 尽管您可以自由使用一段while但是C风格的循环可以无缝处理索引的递增和递减,例如

for ((; i <= j; i++, j--)); do

note the i <= j . 注意 i <= j If you have an odd-number of elements in the array, on your last iteration, you will simply be deleting one-value instead of two. 如果数组中的元素数为奇数,则在最后一次迭代中,您将只删除一个值而不是两个值。 To handle that condition you need a simple test within the loop to check whether [ "$i" = "$j" ] (or using the arithmetic comparison (( i == j )) ). 为了处理这种情况,您需要在循环中进行一个简单的测试,以检查[ "$i" = "$j" ] (或使用算术比较(( i == j )) )。

Putting that altogether, you could refine your element removal to empty the array two-elements at a time to something similar to the following: 综上所述,您可以优化元素删除,以一次清空数组两个元素,使其类似于以下内容:

#!/bin/bash

elegidos=(1 2 3 4 5 6 7)
i=0
j=$((${#elegidos[@]} - 1))
delete=($i $j)

for ((; i <= j; i++, j--)); do
    declare -p elegidos
    unset elegidos[$i]
    [ "$i" != "$j" ] && unset elegidos[$j]
    delete=($i $j)
done

Example Use/Output 使用/输出示例

$ bash array_del.sh
declare -a elegidos='([0]="1" [1]="2" [2]="3" [3]="4" [4]="5" [5]="6" [6]="7")'
declare -a elegidos='([1]="2" [2]="3" [3]="4" [4]="5" [5]="6")'
declare -a elegidos='([2]="3" [3]="4" [4]="5")'
declare -a elegidos='([3]="4")'

You can see above, that on the first 3-iterations, both end-elements are removed. 您可以在上面看到,在前三个迭代中,两个末端元素均被删除。 However, notice on the last removal (there originally being and odd number of elements, only the single-last value is removed on the final iteration. 但是,请注意最后一次删除(元素最初为且为奇数 ,在最后一次迭代中仅删除了倒数第二个值)。

Look things over and let me know if I captured what you were attempting and whether you have any further questions. 查看情况,让我知道我是否掌握了您的尝试以及是否还有其他问题。 If your intent was something else, drop a comment and I'm happy to help further. 如果您还有其他意图,请发表评论,我们很乐意为您提供进一步的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM