在bash中从数组中删除元素

时间:2016-02-08 10:11:01

标签: arrays bash

所以我只需要删除数组中的匹配对。我的阵列包括一副牌。如果元素是一对,例如,如果数组$ sortedHand是:SA DA C9 C8

因为它们是黑桃和钻石中的一对A ..我需要从数组$ sortedHand

中删除它

那么新的变量可能是$ removedHand只包含C9 C8。希望你理解

#!/bin/bash

declare -a cards=(null SA HA DA CA SK HK DK CK SQ HQ DQ CQ SJ HJ DJ CJ ST HT DT CT S9 H9 D9 C9 S8 H8 D8 C8 S7 $
declare -a sortedHand

for i
do
    hand+=' '${cards[i]}
done

set -- $(printf "%d\n" "$@" | sort -n)

for i
do
    sortedHand+=' '${cards[i]}
done

echo The hand is $hand
echo Sorted hand $sortedHand

编辑我添加了代码

#!/bin/bash

declare -a cards=(null SA HA DA CA SK HK DK CK SQ HQ DQ CQ SJ HJ DJ CJ ST HT DT CT S9 H9 D9 C9 S8 H8 D8 C8 S7 $

for i
do
    hand+=' '${cards[i]}
done

set -- $(printf "%d\n" "$@" | sort -n)

for i
do
    sortedHand+=' '${cards[i]}
done

echo The hand is $hand
echo Sorted hand $sortedHand

for((i=0; i -le ${#hand}; ++i))
do
    for((j=0; j -le ${#hand}; ++j))
    do
            if [ $i == $j ]
            then continue
            fi
            if [ ${hand[i]:1:1} == ${hand[j]:1:1} ]
            then continue 2
            fi
    done
    sortedHand+=' '${hand[i]}
done

echo Remaining cards $sortedHand

这是我得到的输出

turtle.sh 1 2 5 10
The hand is SA HA SK HQ
Sorted hand SA HA SK HQ
./turtle.sh: line 23: ((: i -le 12: syntax error in expression (error token is "12")
Remaining cards SA HA SK HQ

请帮助

另一个编辑

#!/bin/bash

declare -a cards=(null SA HA DA CA SK HK DK CK SQ HQ DQ CQ SJ HJ DJ CJ ST HT DT CT S9 H9 D9 C9 S8 H8 D8 C8 S7 $

for i
do
    hand+=' '${cards[i]}
done

set -- $(printf "%d\n" "$@" | sort -n)

for i
do
    sortedHand+=' '${cards[i]}
done

echo The hand is $hand
echo Sorted hand $sortedHand

for((i=0; i-le${#sortedHand}; ++i))
do
    for((j=0; j-le${#sortedHand}; ++j))
    do
            if [ $i == $j ]
            then continue
            fi
            if [ ${sortedHand[i]:1:1} == ${sortedHand[j]:1:1} ]
            then continue 2
            fi
    done
    remainingHand+=${hand[i]}
done

echo Remaining cards $remainingHand

上述代码的结果

turtle.sh 1 2 5 10
The hand is SA HA SK HQ
Sorted hand SA HA SK HQ
Remaining cards

感谢您的帮助

还尝试了您的确切代码

turtle.sh 1 2 5 10
The hand is SA HA SK HQ
Sorted hand SA HA SK HQ
Remaining cards SA HA SK HQ

2 个答案:

答案 0 :(得分:0)

要从数组中删除元素,请使用unset命令(带引号以避免路径名扩展):

#!/bin/bash

declare -a cards=(null SA HA DA CA SK HK DK CK SQ HQ DQ CQ SJ HJ DJ CJ ST HT DT CT S9 H9 D9 C9 S8 H8 D8 C8 S7 $)

echo "${cards[@]}" # null SA HA DA CA SK HK DK CK SQ HQ DQ CQ SJ HJ DJ CJ ST HT DT CT S9 H9 D9 C9 S8 H8 D8 C8 S7 $
unset 'cards[1]'
echo "{cards[@]}" # null HA DA CA SK HK DK CK SQ HQ DQ CQ SJ HJ DJ CJ ST HT DT CT S9 H9 D9 C9 S8 H8 D8 C8 S7 $

答案 1 :(得分:0)

循环遍历hand并从外部循环的当前索引再次循环hand。 比较元素的第二个字母,如果未找到匹配项,则将索引添加到sortedHand

看起来像这样:

cards=(null SA HA DA CA SK HK DK CK SQ HQ DQ CQ SJ HJ DJ CJ ST HT DT CT S9 H9 D9 C9 S8 H8 D8 C8 S7)

 for i in $@ # $@ are all passed arguments, if this gives issues try putting quotes around it
do hand+=(cards[i])
done

for((i=0; i < ${#hand[@]}; ++i)) # C style loop, uses i and <, not $i and -le.
do
    for((j=0; j < ${#hand[@]}; ++j)) # ${#hand[@]} is the number of elements in the array hand. 
    do
        if [ $i == $j ]
        then continue
        fi
        if [ ${hand[i]:1:1} == ${hand[j]:1:1} ]
        then continue 2
        fi
    done
    sortedHand+=(${hand[i]})
done

echo ${hand[@]} # ${hand[@]} is the array in string representation.
echo ${sortedHand[@]}