Dictionary usage question..

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Dictionary usage question..

Rick Flower-2
Ok.. Next stumbling block that has me stumped..

I've got an attributes dictionary with some XML stuff in it
using the method outlined in the XML page for GST (using the
supplied dictionaryForAttributes method... Below is the populated
dictionary

An instance of Dictionary
  tally: 2
  contents: [
  1->'label'->'label-Name-1'
  2->'link'->'http://foobar.com'
  ]

When I try to access it as follows it throws an error :

labelName := attributes at: 'label'.
Object: Dictionary new: 8 "<-0x53bb4d0>" error: Invalid argument label:
key not found

As far as I can tell I'm using it correctly.. Any ideas?

_______________________________________________
help-smalltalk mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: Dictionary usage question..

Rick Flower-2
Ok.. I changed the way the dictionary was created and
it's now being done as follows to eliminate the association,
just to simplify.  If it matters I'm using version 3.2 of GST..

dictionaryForAttributes: aCollection [
        | dict |
        dict := Dictionary new.
        aCollection do: [:each | dict at: each key put: each value ].
        ^dict.
" ^Dictionary withAll: (aCollection
                collect: [ :each | each key type -> each value ])"
]

Here's the code that populates it from an array of XML.Attributes:
        attributes := self dictionaryForAttributes: (each attributes).

Here's the results of what's now in the attributes dictionary :
        attributes inspect.

An instance of Dictionary
   tally: 2
   contents: [
     {link}->'http://foobar.com'
     {label}->'use-case-Name-1'
   ]

Here's the code that is trying to do a lookup for an item in the
dictionary..

        labelName := attributes at: 'label'.

And here's the results..

Object: Dictionary new: 32 "<-0x53bb440>" error: Invalid argument
label: key not found
SystemExceptions.NotFound(Exception)>>signal (ExcHandling.st:254)
SystemExceptions.NotFound(Exception)>>signal: (ExcHandling.st:264)
SystemExceptions.NotFound class>>signalOn:what: (SysExcept.st:758)
[] in Dictionary>>at: (Dictionary.st:139)
Dictionary>>at:ifAbsent: (Dictionary.st:149)
Dictionary>>at: (Dictionary.st:138)

I just don't get it.. I must be missing something here..

--Rick

_______________________________________________
help-smalltalk mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: Dictionary usage question..

Paolo Bonzini-2
Il 27/09/2012 03:39, Rick Flower ha scritto:

> Ok.. I changed the way the dictionary was created and
> it's now being done as follows to eliminate the association,
> just to simplify.  If it matters I'm using version 3.2 of GST..
>
> dictionaryForAttributes: aCollection [
>     | dict |
>     dict := Dictionary new.
>     aCollection do: [:each | dict at: each key put: each value ].
>     ^dict.
> "    ^Dictionary withAll: (aCollection
>         collect: [ :each | each key type -> each value ])"
> ]
>
> Here's the code that populates it from an array of XML.Attributes:
>     attributes := self dictionaryForAttributes: (each attributes).
>
> Here's the results of what's now in the attributes dictionary :
>     attributes inspect.
>
> An instance of Dictionary
>   tally: 2
>   contents: [
>     {link}->'http://foobar.com'
>     {label}->'use-case-Name-1'
>   ]

The braces are the important part here.  These are not string, they are
NodeTag objects (it is documented: try "XML.Attribute comment").  Thus
you need:

aCollection do: [:each | dict at: each name asString put: each value ].

to build the dictionary.

Paolo

> Here's the code that is trying to do a lookup for an item in the
> dictionary..
>
>     labelName := attributes at: 'label'.
>
> And here's the results..
>
> Object: Dictionary new: 32 "<-0x53bb440>" error: Invalid argument label:
> key not found
> SystemExceptions.NotFound(Exception)>>signal (ExcHandling.st:254)
> SystemExceptions.NotFound(Exception)>>signal: (ExcHandling.st:264)
> SystemExceptions.NotFound class>>signalOn:what: (SysExcept.st:758)
> [] in Dictionary>>at: (Dictionary.st:139)
> Dictionary>>at:ifAbsent: (Dictionary.st:149)
> Dictionary>>at: (Dictionary.st:138)
>
> I just don't get it.. I must be missing something here..



_______________________________________________
help-smalltalk mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: Dictionary usage question..

Rick Flower-2
On 26.09.2012 23:44, Paolo Bonzini wrote:

> The braces are the important part here. These are not string, they
> are
> NodeTag objects (it is documented: try "XML.Attribute comment"). Thus
> you need:
>
> aCollection do: [:each | dict at: each name asString put: each value
> ].
>
> to build the dictionary.

Paolo.. You are correct -- as usual!  I didn't see that forest for the
trees either.. Your code above worked great and got me past the sticky
part and the above "asString" can be changed to "type" and works fine
that way as well.. One thing if you don't mind.. Can you please clue me
in on why the version on the XML GST page won't work as claimed?  The
code is below and does use associations -- however, I thought I saw
code
to deal with associations in Dictionary's when I was perusing the
source
yesterday -- perhaps not? :

^Dictionary withAll: (aCollection
        collect: [ :each | each key type -> each value ])

This results in the following Dictionary object for the earlier
data :

An instance of Dictionary
   tally: 2
   contents: [
     1->'label'->'use-case-Name-1'
     2->'link'->'http://foobar.com'
   ]

While I do have a functional workaround I'm just curious and would like
to update that part of the GST docs if the examples are incorrect..
Thanks again for saving my bacon!

--Rick

_______________________________________________
help-smalltalk mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-smalltalk