Skip to main content
Inspiring
December 3, 2012
Answered

Help using XML as Dictionary key

  • December 3, 2012
  • 1 reply
  • 996 views

I'm not quite understanding the key in Dictionaries. I thought if the same key was passed twice it would still only create one pair in the dictionary. Can anyone explain this to me?

With a key of a string or number it seems to work as I would expect, but the XML as key doesn't seem to work. The line in there that changes the title was just to check if the XML was being passed as a reference or value. It is a reference. So it seems like the dictionary should know the key is the same. Why doesn't it?

I'm trying to create an index of the content in some XML Nodes and want to be sure that various nodes don't get added to the index more than once. I also My thinking was to use the XML node itself as as the key. It is the only thing I have in the data that is for sure unique. Is there a way to do what I want to do?

var xmlData:XML=<data>

<session>

<title>Document 1</title>

<desc> <![CDATA[blah blah blah]]></desc>

</session>

<session>

<title>Document 2</title>

<desc> <![CDATA[blah blah blah]]></desc>

</session>

</data>;

var d:Dictionary=new Dictionary();

var xml1:XML=xmlData.session[0];

var xml2:XML=xmlData.session[0];

addToDictionary(xml1);

addToDictionary(xml2);

xmlData.session[0].title="New title";

for(var a:* in d){

          trace(a.title+": "+d);

}

function addToDictionary(xml:XML){

          d[xml]=int(Math.random()*500);

}

traces

New title: 2

New title: 135

This topic has been closed for replies.
Correct answer kglad

because xml1 is not the same variable name as xml2 and the variable name (not the variable's value) is the dictionary key.

i'm not sure what exactly you want to do but this might work for you:

function addToDictionary(xml:XML) {

    d[xml.toString()]=xml;

}

1 reply

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
December 3, 2012

because xml1 is not the same variable name as xml2 and the variable name (not the variable's value) is the dictionary key.

i'm not sure what exactly you want to do but this might work for you:

function addToDictionary(xml:XML) {

    d[xml.toString()]=xml;

}

RothrockAuthor
Inspiring
December 3, 2012

If I change the assigment to:

var xml1:XML=xmlData.session[0];

var xml2:XML=xml1;

addToDictionary(xml1);

addToDictionary(xml2);

Then it does work as I had hoped it might. In any event...

I'm trying to make a (forward) index of some properties of each node in an XML—total word count, frequency of each word, etc. So later when my reverse index returns several XMLs I can quickly look up those data and compute a tf-idf for relevance ranking.

I'm thinking the conversion toString() will slow the whole thing down? I'll give it a look-see.

RothrockAuthor
Inspiring
December 3, 2012

And of course it just occurred to me that I am using XML nodes in my inverse index. I'm going to have to rethink this whole thing....darn...