de en es fr
Let the machine help
Light teasing, exhibition, BDSM, sissyfication, watersports... with sounds and pictures


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Getting a specific value from an array?
PostPosted: Fri Sep 11, 2015 5:21 pm 
Offline
Veteran
User avatar
Sorry, last question for now... my brain must have gone to sleep tonight but is there a function to get a specific value from an array, as chosen by the user?

For example, if I ask:
Code:
def color = getSelectedValue("Which color ?", ["Red", "White", "Black"])

show("You chose "+color+".")

I want the value "color" will be either "Red", "White" or "Black" and NOT "0", "1" or "2" (as getSelectedValue will give me).

Right now, I'm doing:
Code:
def colorArray = ["Red", "Blue", "Yellow"]
def pickColor = getSelectedValue("Pick a color:", colorArray)
def colorChosen = colorArray[pickColor]
show("You picked "+colorChosen+".")

... but I was thinking there was possibly a simple function that does this without all the extra code.

I thought I recalled there being a function like "getSelectedString()" that did this, but that gives me errors so I'm clearly remembering wrong.

Is the way I'm doing it right, or is there a less code-heavy and simple/easy method that I've forgotten?


Last edited by Banjo on Fri Sep 11, 2015 6:16 pm, edited 1 time in total.

Top
 Profile Send private message 
 
 Re: Getting a specific value from an array?
PostPosted: Fri Sep 11, 2015 5:53 pm 
Offline
Site Admin
User avatar
No, your code is the best one ; you can do slighly shorter if you want :
Code:
def colorArray = ["Red", "Blue", "Yellow"]
def pickColor = getSelectedValue("Pick a color:", colorArray)
show("You picked "+colorArray[pickColor]+".")

Or again shorter :
Code:
def colorArray = ["Red", "Blue", "Yellow"]
show("You picked "+colorArray[getSelectedValue("Pick a color:", colorArray)]+".")


Top
 Profile Send private message 
 
 Re: Getting a specific value from an array?
PostPosted: Fri Sep 11, 2015 6:20 pm 
Offline
Veteran
User avatar
doti wrote:
No, your code is the best one ; you can do slighly shorter if you want :
Code:
def colorArray = ["Red", "Blue", "Yellow"]
def pickColor = getSelectedValue("Pick a color:", colorArray)
show("You picked "+colorArray[pickColor]+".")

Or again shorter :
Code:
def colorArray = ["Red", "Blue", "Yellow"]
show("You picked "+colorArray[getSelectedValue("Pick a color:", colorArray)]+".")


Thanks, mate! I was at first worried this couldn't be done... then figured out the code I was using in the first post. And yes, yours is more elegant (and definitely what I'd use in the above examples).

Actually, mine has that extra step/line because (though I didn't say) I actually don't need to tell the user what was picked, but rather *do* need to set "chosenColor" to a string for later use by the script. :)

I will also add this snippet to my tutorial script, as it's a really helpful thing to know.


Top
 Profile Send private message 
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC + 1 hour [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  


Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Maroon Fusion theme created by Oxydo
Software, theme modifications, phpBB modification by Doti 2010 - 2020
This website uses session cookies only.