Register to post in forums, or Log in to your existing account
 

Play RetroMUD
Post new topic  Reply to topic     Home » Forums » zMUD General Discussion
Caldur
Newbie


Joined: 22 Aug 2006
Posts: 5
Location: Rogers, AR

PostPosted: Tue Aug 22, 2006 2:27 am   

Array or String List?
 
I've read a lot about arrays and string lists - arrays in the zMUD help files, and string lists here on the forums. I set up a few arrays, but when I go to the variable list I see that my supposed "arrays" are now called "string lists." Very confusing.

Anyway!

I am designing a potion-switching bot that will automatically switch to a new bottle after my current potion bottle goes empty. It basically works like this:

1. Invoking alias "hp" sets @lastpot to "HEALTH" and takes a sip of my health potion bottle - "@healthpot." I changed @healthpot to an array to contain all of the bottle numbers, which is supposed to be referenced by the index variable @hpindex. It should look like "sip @healthpot.hpindex"
2. If I attempt to drink an empty bottle, and the alias set @lastpot to HEALTH, my trigger will pick up the error and #ADD hpindex 1. This in theory should increase the index of the array I'm referencing, and switch me to the next bottle.

This however is not the case. It references the first value of the array if I'm lucky, and no more. I can use @healthpot.0, @healthpot.1, @healthpot.3, and so on to reference the array values, but that defeats the purpose of having an array in the first place.

I've read that string lists are better than arrays. If someone could tell me how I can access specific elements within a string list with a variable index, I would very much appreciate it. Thanks!
Reply with quote
MattLofton
GURU


Joined: 23 Dec 2000
Posts: 4834
Location: USA

PostPosted: Tue Aug 22, 2006 2:45 am   
 
%item(@stringlist,@variableindex)

ZMud doesn't support the @stringlist.@index syntax, so you have to do it like above.
_________________
EDIT: I didn't like my old signature
Reply with quote
Caldur
Newbie


Joined: 22 Aug 2006
Posts: 5
Location: Rogers, AR

PostPosted: Tue Aug 22, 2006 2:51 am   
 
Ok, I tried "sip %ITEM(@healthpot,@hpindex)"
That didn't work. I tried assigning a variable to the return value of %ITEM(@healthpot,@hpindex) as such:
#VARIABLE myvar %ITEM(@healthpot,@hpindex)
Still nothing.

Do I have the syntax wrong, or did I misunderstand you?
Reply with quote
Qiz
Novice


Joined: 14 Aug 2006
Posts: 34
Location: Sweden

PostPosted: Tue Aug 22, 2006 2:40 pm   
 
It looks right to me. 3 things might screw things up though:
1. @healthpot contains no data
2. @hpindex contains no data
3. @hpindex is out of bounds

Just before the "sip %ITEM(@healthpot,@hpindex)" line, insert
#SHOW @healtpot
#SHOW @hpindex
If either one is empty, or if @hpindex indexes beyond the number of items in @healthpot you have your reason.

If you need to do bounds checking on @hpindex, use
%itemnum(@healthpot)
Reply with quote
Caldur
Newbie


Joined: 22 Aug 2006
Posts: 5
Location: Rogers, AR

PostPosted: Tue Aug 22, 2006 11:22 pm   
 
I work as a programmer, so I am very familiar with arrays and indexes. The logic behind them is not my problem, the zMUD syntax for referencing array elements is. An out of bounds index or null array element would be a common sense error, but just for confirmation, this is what my variables yield from invoking the #SHOW function. (#SHOW of course will not work with an array variable, #SHOWARR must be used in its stead).

#SHOW hpindex yields 3
#SHOWVAR healthpot yields Array healthpot = 0:"bottle45119", 1:"bottle45230", 2:"bottle45291", 3:"bottle48256", 4:"bottle51606"

If it turns out I can't reference array elements by a variable index I'll have to devise a work-around. I would much rather use an array though, so everyone please continue to leave advice.
Reply with quote
MattLofton
GURU


Joined: 23 Dec 2000
Posts: 4834
Location: USA

PostPosted: Wed Aug 23, 2006 12:09 am   
 
I suggest using stringlists. Stringlists may look the same as arrays, but they are not functionally identical. Stringlists have a lot of commands and functions that work with/were designed for them; arrays don't (in fact, I think you might only have like half a dozen to work with). If I recall, arrays get deleted when you close down the character session; stringlists do not. Arrays are typed values, while stringlists are considered to have no type (just like regular zmud variables and datarecord variables).

The syntax I gave does indeed work perfectly with stringlists, which is what you initially asked about. Apparently the miunderstanding stems from your interchanging of the terms.
_________________
EDIT: I didn't like my old signature
Reply with quote
Caldur
Newbie


Joined: 22 Aug 2006
Posts: 5
Location: Rogers, AR

PostPosted: Wed Aug 23, 2006 1:45 am   
 
Ok. This is how I have defined my array:
#VARIABLE healthpot %array(bottle12345,bottle23456,bottle34567,bottle45678,bottle56789)

This is what I'm going to try. Correct me if I'm wrong.
1.) Delete the "healthpot" variable, since it is an array and not a stringlist (which is why the %item(healthpot,hpindex) method does not work, I'm assuming).
2.) Create a new healthpot variable, then #ADDI each bottle (e.g., #ADDI healthpot bottle12345 <enter> #ADDITEM healthpot bottle23456 <enter> so on and so forth...). Adding the items individually is no problem, but is there a way to add multiple items at once? That would be nifty.
3.) After creating my new stringlist data type healthpot variable, I should be able to access it with %item(@healthpot,@hpindex).

If this doesn't work for some reason I'll be back :P
Reply with quote
Caldur
Newbie


Joined: 22 Aug 2006
Posts: 5
Location: Rogers, AR

PostPosted: Wed Aug 23, 2006 2:05 am   
 
Alright, it's done. #SHOW healthpot now yields bottle45230|bottle45291|bottle45119|bottle48256|bottle51606. So, I should be able to type "sip %item(@healthpot,@hpindex)". It doesn't allow me to do this though. It returns with the message:
You see no "%item(@healthpot,@hpindex)" to drink or sup from.
So zMUD is sending the entire %item command to the MUD without interpreting it. I've tried the following to access it:
%item(@healthpot,2) - this should bring up bottle45119
I've tried %item("bottle45230|bottle45291|bottle45119|bottle48256|bottle51606",2) but this brings up nothing either.
I'm typing these in directly at the command line. Do I need to insert these in an alias for them to work correctly? Or is there some sort of setting I need to change? It looks syntactically correct, according to the descriptions I've seen here and the descriptions in the help files.

Any ideas?
Reply with quote
MattLofton
GURU


Joined: 23 Dec 2000
Posts: 4834
Location: USA

PostPosted: Wed Aug 23, 2006 2:07 am   
 
There is.

#additem Healthpot "item to add #1|item to add #2|item to add #etc"

Doing it this way will surround everything in the above quotes in parentheses, like so:

existing item 1|existing item2|(item to add #1|item to add #2|item to add #etc)

Fortunately, a stringlist is entirely a text entity and not a control-code-delimited one like datarecord variables so the parentheses are easily removed if you don't want or need nested stringlists:

Healthpot = %remove(%remove(@Healthpot, "(", ""), ")", "")
_________________
EDIT: I didn't like my old signature
Reply with quote
Display posts from previous:   
Post new topic   Reply to topic     Home » Forums » zMUD General Discussion All times are GMT
Page 1 of 1

 
Jump to:  
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 vote in polls in this forum

© 2009 Zugg Software. Hosted by Wolfpaw.net