Simulating Arrays


#1

I was reading the following discussion:
https://groups.google.com/group/choicescript/tree/browse_frm/month/2010-04/f5bdb2586804305b?rnum=1&_done=%2Fgroup%2Fchoicescript%2Fbrowse_frm%2Fmonth%2F2010-04%3F

And was intrigued by this part (2nd post):

So you could, for example define a whole set of variables like Army1Type,
Army1Number, Army1Weapon, Army1Armor, Army1Horse, Army2Type, etc.
You can then do things like:
*set CurrentArmy 1 How do you improve your army? *choice #Buy enchanted swords *setref "Army"&{CurrentArmy}&"Weapon" "EnchantedSwords" *finish #Buy enchanted armor *setref "Army"&{CurrentArmy}&"Armor" "EnchantedArmor" *finish #Buy unicorns *setref "Army"&{CurrentArmy}&"Horse" "Unicorns" *finish #Recruit more troops *setref "Army"&{CurrentArmy}&"Number" +50 *finish

*setref “Army”&{CurrentArmy}&“Weapon” “EnchantedSwords”
^ That doesn’t work it gives me an error saying it expected a parenthesis (bracket), number, string or etc NOT an “operator”.

Is that because - currently - choicescript isn’t able to do that, or is the formatting of the quoted code just a little bit out?

I’m assuming that is posted by a c/s developer, so I’m pretty shocked by the fact that it doesn’t work. (I’ve copied the code 100%)

If anyone can shed some light on this, I’d be very much appreciative. :slight_smile:


#2

Maybe the & is bugging it up. I think that’s referred to as an operator in CS. IIRC *setref doesn’t always work like *set, in that a few things are non-functional.


#3

@CJW, try this:
*temp a *temp Army1Weapon *temp ArmyWeapon *set Army1Weapon "Sword" *set a 1 *set ArmyWeapon ("Army"&a)&"Weapon" *setref ArmyWeapon "Enchanted Sword" Army1Weapon = ${Army1Weapon}


#4

Yes! Hah. That’s what we’re talking about.
Thank you @eposic, that works a treat :slight_smile:


#5

@CJW, I just tried doing *setref varname %+50 and *setref varname +50 but CS doesn’t like it. It will handle *setref varname 50 just fine. It doesn’t appear to be able to do any sort of operation during a *setref, only an assignment. Makes it rather limited for use in simulating arrays of numeric data in any way other than simply accessing static values. Have you been able to do something like what I tried?


#6

@eposic, try:

*setref varname {varname}+50 and *setref varname {varname}%+50.


#7

@P_Tigras, thank you bunches! You just answered another question I had, too, which was how to access the value of the referenced variable. I’ve been using ${var} to display the value of a variable, but hadn’t caught on to the {var} syntax. This is going to make some things a lot easier to deal with than writing loads of *if statements. Thanks again!


#8

@eposic You’re most welcome. *setref & {var} are critical to an inventory API I was writing in CS before I lost power for nearly 6 days this past week. Now that I have power back I’ll hopefully get around to finishing that this weekend. I’m planning on combining my inventory API with @stainedofmind’s global code for major coolness.


#9

Sorry guys…, yeah the syntax for setref is an absolute mess. Lots of trial and error, I’ll post up some of my own examples if you like. It’s very useful, but takes some trial and error and additional work arounds in a lot of cases.


#10

You have nothing to apologize for @CJW. While I wouldn’t call the syntax for *setref a complete mess, it isn’t particularly intuitive either. And I had to resort to workarounds as well to get around the limitations of CS when writing my inventory api which utilizes a a pair of faux arrays to keep track of inventory items and their weights.


#11

Well it’s not *setref specifically, but when you’re trying to do something like this:

*set profileid {(“firstname”&({((“wd_id”&profile)&("_"&n))}))}&((" -> ")&({“firstname”&profile}))

As you can imagine, it can get quite confusing… ^^’


#12

uggh, that’s painful. Yep, I can see where you’re coming from.


#13

ow. :open_mouth:


#14

Indeed ^^
To their credit though, it DOES work and it is necessary/useful for certain applications. There’s a pattern too but it can still take some heavy trial and error to get the exact syntax right…ugh