Remove elements from an array in bash -


so need remove matching pairs in array. array consists of deck of cards. if element pair, example if array $sortedhand is: sa da c9 c8

because pair of aces in spades , diamonds. need remove array $sortedhand

so new variable maybe $removedhand contain c9 c8. hope 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      hand+=' '${cards[i]} done  set -- $(printf "%d\n" "$@" | sort -n)      sortedhand+=' '${cards[i]} done  echo hand $hand echo sorted hand $sortedhand 

edit 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 $      hand+=' '${cards[i]} done  set -- $(printf "%d\n" "$@" | sort -n)      sortedhand+=' '${cards[i]} done  echo hand $hand echo sorted hand $sortedhand  for((i=0; -le ${#hand}; ++i))     for((j=0; j -le ${#hand}; ++j))                 if [ $i == $j ]             continue             fi             if [ ${hand[i]:1:1} == ${hand[j]:1:1} ]             continue 2             fi     done     sortedhand+=' '${hand[i]} done  echo remaining cards $sortedhand 

this output get

turtle.sh 1 2 5 10 hand sa ha sk hq sorted hand sa ha sk hq ./turtle.sh: line 23: ((: -le 12: syntax error in expression (error token "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 $      hand+=' '${cards[i]} done  set -- $(printf "%d\n" "$@" | sort -n)      sortedhand+=' '${cards[i]} done  echo hand $hand echo sorted hand $sortedhand  for((i=0; i-le${#sortedhand}; ++i))     for((j=0; j-le${#sortedhand}; ++j))                 if [ $i == $j ]             continue             fi             if [ ${sortedhand[i]:1:1} == ${sortedhand[j]:1:1} ]             continue 2             fi     done     remaininghand+=${hand[i]} done  echo remaining cards $remaininghand 

the outcome of above code

turtle.sh 1 2 5 10 hand sa ha sk hq sorted hand sa ha sk hq remaining cards 

thanks help

also tried exact code

turtle.sh 1 2 5 10 hand sa ha sk hq sorted hand sa ha sk hq remaining cards sa ha sk hq 

loop through hand , loop through hand again current index of outer loop. compare second letter of elements , add index sortedhand if no matches found.

that 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)   in $@ # $@ passed arguments, if gives issues try putting quotes around hand+=(cards[i]) done  for((i=0; < ${#hand[@]}; ++i)) # c style loop, uses , <, not $i , -le.     for((j=0; j < ${#hand[@]}; ++j)) # ${#hand[@]} number of elements in array hand.              if [ $i == $j ]         continue         fi         if [ ${hand[i]:1:1} == ${hand[j]:1:1} ]         continue 2         fi     done     sortedhand+=(${hand[i]}) done  echo ${hand[@]} # ${hand[@]} array in string representation. echo ${sortedhand[@]} 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -