I have a list, defined thusly:
var/tmp/list/my_list = new()
It's global, but not that matters here. I want to add to that list an association. I have worked with list associations before, but this particular one is bugging me a bit. Ok, so, I have my list, I want to use the value of a variable as the unique text identifier for the index. I want to associate with this another text string, also stored in a variable. This seems pretty straight forward to me, but I still recieve a runtime error when the code hits the line that adds the association to the list:
my_list["[my_unique_text]"] = "[some_other_text]"
Any ideas? I could use an object reference in place of the unique text, and I did try that, but I still recieved that nasty runtime error:
my_list[MY_OBJ] = "[some_other_text]"
It seems no matter how I try to setup the association, I get the Bad Index error. I sure could use some advice on this. Thanx!
~X
ID:148662
Nov 21 2002, 5:09 am
|
|
In response to Skysaw
|
|
I tried that as well, with the same results. I tried:
var/tmp/list/my_var[] for the association itself, I tried: my_list[MY_OBJ] = "[some_other_text]" And other variations with the same result. ~X |
I don't see why you are getting a problem. Isn't something like this what you're after:
var/tmp/list/my_list = new Works just fine. Did I miss the problem? |
In response to Malver
|
|
Malver wrote:
Works just fine. Did I miss the problem? That's the problem, it doesn't work just fine for my code. I see nothing different between mine and yours, except mine gives me a runtime error. The problem must lie elsewhere then. Could it be the object reference is bad? If that's the case, how does it successfully delete the object only a few lines later? It does this, that works. I even tried spawn(10) before deleting it, thinking maybe it wasn't there when the list association was trying to read it, and thus trying to use null as the index, which would give me a bad index runtime error. Still got the error with the spawn(10), though. I don't know. :\ It's perplexing, to say the least. ~X |
In response to Skysaw
|
|
Skysaw wrote:
I think it may be the list declaration itself. If it thinks it's not an associated list, it will only want positive integers as indexes. Try declaring it like this: It's because you aren't creating it. Add a " = new" to that line, and it works just fine. |
In response to Xooxer
|
|
Xooxer wrote:
Malver wrote: Could you show us that tidbit of code that you suspect the problem is in? |
In response to Malver
|
|
Malver wrote:
Could you show us that tidbit of code that you suspect the problem is in? Sure. This is with alot of irrelivant code cut out, but if you really want to see that, I can post the entire proc. for(var/obj/channel/CHANNEL in all_channels) ~X |
In response to Malver
|
|
Malver wrote:
Skysaw wrote: Associative lists do not need new. Regard the example in the reference. |
In response to Xooxer
|
|
Xooxer wrote:
Malver wrote: for(var/obj/channel/CHANNEL in all_channels) Actually a very relevant piece is missing: The initialization of deleted_chans. Bad index errors are almost always caused by the list actually being null; on rare occasions it might be set to something else by mistake, like a number or a string or a totally different type of object. My guess, therefore, is that deleted_chans has been set--or reset--to null at some point prior to this. Lummox JR |
In response to Skysaw
|
|
Skysaw wrote:
Associative lists do not need new. Regard the example in the reference. I don't believe that's true. Associative lists are functionally no different from regular lists in BYOND--in fact they're exactly the same thing; any regular list can be made associative merely by associating something, and DM doesn't know one from the other. A regular list is merely an associative list with no associations made yet. If you don't set the var to something, it's gonna be null. The example in the reference is therefore incorrect; as far as I can see it shouldn't work. I avoid the [] way of declaring lists anyway because there's too much confusion involved. My preferred method is:
var/list/L=list()
Lummox JR |
In response to Lummox JR
|
|
This is the only other code that deals with deleted_chan.
var/list See anything that would cuase this list to be null? I don't. I have many lists that are handled this way, and all the rest work fine. This isn't even the only associative list I have, it's just the only one that's not working. :( ~X |
In response to Xooxer
|
|
If the list does not exist in the savefile, then the list would be set to null in world/New(). Try adding in this line after you try to load the list:
if(!deleted_chans) deleted_chans = new() |
In response to English
|
|
heh
Yeah, that was it alright. The code still doesn't do what it's supposed to, but at least the runtime error is gone. Thanx English, Lummox, Malver and Skysaw! I was begining to question my sanity... well, I always question that. I think I have this whole associative list thing ingrained in my skull a bit deeper now, but I won't complain. :) ~ Happy Xoox :D <s>YES!!!</s> |
In response to Lummox JR
|
|
Lummox JR wrote:
Skysaw wrote: I've used the form in the reference, and it works just fine, actually. Actually, I've used it a number of times in several different projects, and I've never had any trouble with it. |
var/tmp/list/my_list[]