But since it's not in POSIX, do not put “#!/bin/sh” at the beginning of your script.

Lucien Lachance

08-18-2013 10:56 AM

I have an associative array called move_set, how can I just get 'r' 'p' 's' at random?

Code:

declare -A move_set=(['r']="rock" ['p']="paper" ['s']="scissors")

ntubski

08-18-2013 11:06 AM

Using PTrenholme's code, replace

Code:

$(shuf -i 1-$# -n 1)

with

Code:

$((RANDOM % $# + 1))

PTrenholme

08-18-2013 12:46 PM

To clarify, ${move_set[*]} is the list of the values in move_set, and ${!move_set[*]} is the list of the key values. See "info bash" in the "Parameter Expansion" section in the "Basic Shell Features" group for details.

Oh, because I initially coded the while loop to just get the structure of the game setup. It's almost like a roadmap, I can't go anywhere unless I know how the game will be setup. Now I'm going to add things in between where I'm receiving a move from player. After that I'll validate the move and see if it's a key of move_set and so forth.. Sorry for leaving that out. I originally just wrote this in Python and thought it would be fun to port it to Bash.

grail

08-19-2013 08:09 PM

Yes a comma converts the first / single character to lower case, see here for further details.

Quote:

And I'm still having trouble pulling out a random key from move_set.

You might have to explain further what trouble you are referring to?

Lucien Lachance

08-19-2013 09:38 PM

@grail in Python it was really simple to pick out a key from a list tuple. However, I'm working with an associative array in Bash. Here I've made a function that gets a move from player and opponent makes a random decision of either 'r', 'p', or 's'. For some reason I'm not understanding how to do this same thing in bash.