I'm writing a Texas Hold'em script in bash v3.00.16(1) to learn more about awk/nawk scripts and regex expressions by trying to randomize a list of names using awk's rand function. The problem is that the elements in the var convert to a single element in the nawk script. I've tried several things, including separating the names with commas, single and double quotes but no joy.
The result is that ${Player_Names[0]} contains all the names and ${#Player_Names[@]} = 1.
I started with some code by C. F. Johnson that randomizes a deck of cards and wanted to alter that to randomize a list of player names. I replaced the regex expression {2,3,4,5,6,7,8,9,J,Q,K,A}_{Hearts,Spades,Diamonds,Clubs} that generates the deck of cards with a variable that contains a list of names. Note: In my variable there are 124 names.
xNAMES=( Abigail Alejandro Alex Alton Amos )
get_players() # USAGE: get_players
{
Player_Names=$(
printf "%s\n" ${xNAMES[@]} |
${uBIN}/nawk '## Seed the random number generator
BEGIN {
srand()
}
## Put a random number in front of each line
{ printf "%.0f\t%s\n", rand() * 99999, $0 }' |
sort -n | ## Sort the lines numerically
cut -f2 ## Remove the random numbers
)
# Player_1 is the user.
# Skipping
Player_2=${xNAMES[2]}
Player_3=${xNAMES[3]}
Player_4=${xNAMES[4]}
Player_5=${xNAMES[5]}
Player_6=${xNAMES[6]}
}
get_players
${BIN}/printf "\n\t${Player_1}\t${Player_2}\t${Player_3}\t${Player_4}\t${Player_5}\t${Player_6}\n"
What am I doing wrong? Do I have to replace the original regex expression (that works) with the list of names in the nawk script for it to parse correctly?
Thanks