-1

I have an issue and this is the result when i call one method in my code.

t do:[:i |
        dict := Dictionary newFrom:{t->items}.
    ].  

t is an

OrderedCollection(#name #lastName #birthDate)

and items too. items an OrderedCollection('cs0yh1n1b2bm0wps16uvy7tfi' 'cs0yh1k1t3bgdszfupe407qdg' 21 April 1975)

a Dictionary [1 item] (an OrderedCollection(#name #lastName #birthDate)->an OrderedCollection('cs0yh1n1b2bm0wps16uvy7tfi' 'cs0yh1k1t3bgdszfupe407qdg' 21 April 1975).

I would like to obtain a gender dictionary

a Dictionary (#name->'cs0yh1n1b2bm0wps16uvy7tfi' #lastNamename->'cs0yh1k1t3bgdszfupe407qdg' #birthDay->21 April 1975)

how can i do this please ?

  • 1
    What have you tried? _How_ are you trying to "obtain a gender dictionary"? As was pointed out to you about a day ago, please read https://stackoverflow.com/help/minimal-reproducible-example and update your question with more details. – Amos M. Carpenter Sep 23 '22 at 07:03

2 Answers2

3

You are creating a new Dictionary each time through the loop. You need to create a new (empty) Dictionary before the loop and add each element using #'at:put:'. If you want to map index 1 in t to index 1 in items, then you need to have a counter. Perhaps 1 to: t size do: [:i | ...].

You are asking a lot of questions about Smalltalk and we are delighted to have you learning it. Have you tried spending a few hours studying a book or taking a course? I recommend the Pharo Mooc, some Pharo books, or some Smalltalk books.

James Foster
  • 2,070
  • 10
  • 15
0

TL;DR Let me re-iterate that Pharo By Example (PBE) is long and detailed and you'll learn a lot by working through that.

That said, sometimes an explicit answer is easier to understand when you're starting out. So here's a more explicit solution to your question. However, please note, that this code has a major weakness which is the size of the keys array has to be equal to or smaller than the size of the values array. Bad things happen if that is not true.

keys := #(#name #lastName #birthDate).
values := #('cs0yh1n1b2bm0wps16uvy7tfi' 'cs0yh1k1t3bgdszfupe407qdg' '21 April 1975').
myDict := Dictionary new.
1 to: ( keys size) do: [ :item | myDict at: ( keys at:item ) put: (values at:item )  ].

In this example the Array which you get from the #() construct, works equally as well as an OrderedCollection. If OrderedCollection is important to you, and you need a soltuion that uses that Object you could replace the first two lines with this:

keys := OrderedCollection withAll: #(#name #lastName #birthDate).
values := OrderedCollection withAll: #('cs0yh1n1b2bm0wps16uvy7tfi' 'cs0yh1k1t3bgdszfupe407qdg' '21 April 1975').

The key piece of information that you missed in your code is that you have two collections which are indexable, and so you have to solve the problem by indexing both of them.

Another problem with your code is each iteration writes over the dictionary.

dict := Dictionary newFrom:{t->items}.

Finally: There's this solution.

myData := Dictionary newFrom: {#name->'cs0yh1n1b2bm0wps16uvy7tfi'. #lastName->'cs0yh1k1t3bgdszfupe407qdg'. #birthDate->'21 April 1975'}

Note: that at the end of each association there's a . that separates the associations and makes it a list of three elements.

The only place where I've simplified the problem is that I've taken your attempt at using a Date, 21 Apr 1975 and turned it into a string. Only because it's simpler not to include the date construction in the example.

Tony Giaccone
  • 511
  • 5
  • 18