Usman Khan February 2016

Remove elements from an array in bash

So I just need to remove matching pairs in an array. My array consists of a deck of cards. If an element is a pair, for example if the array $sortedHand is: SA DA C9 C8

Because their is a pair of aces in spades and diamonds. I need to remove it from the array $sortedHand

So then the new variable maybe $removedHand would only contain C9 C8. Hope you understand

#!/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

Edit I have added code

#!/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

This is the output I get

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

Help please

Another Edit

#!/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 h        

Answers


silverdrop February 2016

To delete an element from an array, use the unset command (with quotes to avoid pathname expansion):

#!/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 $


S.Klumpers February 2016

Loop through hand and loop through hand again from the current index of the outer loop. Compare the second letter of the elements and add index to sortedHand if no matches were found.

That would look like this:

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[@]}

Post Status

Asked in February 2016
Viewed 1,128 times
Voted 14
Answered 2 times

Search




Leave an answer