Var in nawk script parse incorrectly

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

You're telling it to put it in a string, so you get a string. To put it in an array, tell it it's an array.

Player_Names=( $(
printf "%s\n" ${sNAMES[@]} |
  ${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
) )

You can alter what character the array splits on by altering the shell's IFS variable. You might want IFS=$'\n' to split on newlines only. Be sure to put it back once you're done since it affects other things too.

Sorry, I guess I'm missing something. I don't see any difference in what you posted and what I'm doing.

printf "%s\n" ${sNAMES[@]}

## Typo in original post has been fixed. sNames should have been xNames.

By putting parentheses around the names bash recognizes that the contents are an array. I can test this using the following.

$ xNAMES=( Abigail Alejandro Alex Alton Amos )
$ echo ${xNAMES[0]}
Abigail
$ echo ${xNAMES[1]}
Alejandro
$ echo ${xNAMES[2]}
Alex
$ echo ${xNAMES[3]}
Alton
$ echo ${#xNAMES[@]}
5

My problem is that the following happens when using the awk script.

xNAMES=( Abigail Alejandro Alex Alton Amos )
$ echo ${xNAMES[0]}
Abigail Alejandro Alex Alton Amos
$ echo ${xNAMES[1]}
$
$ echo ${xNAMES[2]}
$ echo ${#xNAMES[@]}
1
$

Please let me know if I missed something.

Thanks

Look closer. I even highlighted the differences in red.

Thanks for pointing that out. That did fix the problem.