Discuss Scratch

sudoyum
Scratcher
5 posts

Dictionaries

A dictionary, commonly found in other languages, is not available in Scratch. Dictionaries are basically lists of variables. The difference is names for each of them.

Right now, you can get a specific item in a list by telling them the position of the item you want, like this:
(item (3 v) of [some-list v])

With dictionaries, you would be able to do this:
(item (name v) of [person v])

Which would give you “John”.
mathfreak231
Scratcher
1000+ posts

Dictionaries

Meh, that could be useful, but I don't see too many uses in Scratch, and there's a not-too complicated workaround:

define dictionary index [key]
set [i v] to [1]
repeat until <(item (i) of [keys v]) = (key)>
change [i v] by (1)
end
set [result v] to (item (i) of [values v])
davidkt
Scratcher
1000+ posts

Dictionaries

Support.
AonymousGuy
Scratcher
1000+ posts

Dictionaries

But that would make lists seem more like classes, which they aren't…
davidkt
Scratcher
1000+ posts

Dictionaries

AonymousGuy wrote:

But that would make lists seem more like classes, which they aren't…
*ahem*

Scratch source code wrote:

public class ListCell extends Sprite {
AonymousGuy
Scratcher
1000+ posts

Dictionaries

davidkt wrote:

AonymousGuy wrote:

But that would make lists seem more like classes, which they aren't…
*ahem*

Scratch source code wrote:

public class ListCell extends Sprite {
*sigh*

As in classes in the Scratch language. There are no actual classes in the Scratch language - sprites are similar, but not the same thing. Lists are Arrays in functionality, not classes.
Blueinkproductions
Scratcher
1000+ posts

Dictionaries

Ultra-mega support.
sudoyum
Scratcher
5 posts

Dictionaries

AonymousGuy wrote:

But that would make lists seem more like classes, which they aren't…

If you've ever programmed in python, dictionaries and classes are defined differently:


# This is a dictionary:
person = {“name”:“John”, “age”:5555, “food”:“pizza”}

#This is a class:
class person(object):
name = “John”
age = 5555
food = “pizza”

#The values of the two are retrieved differently:

#For dictionary:
person

#For class:
person().name



# Means a comment

AonymousGuy wrote:

Lists are Arrays in functionality, not classes.

True. Lists are arrays. In python, Lists are also arrays.



AonymousGuy
Scratcher
1000+ posts

Dictionaries

sudoyum wrote:

If you've ever programmed in python, dictionaries and classes are defined differently:
I have in fact programmed in Python, but I really don't like the language because of… uh… most of the syntax and stuff, so…

In a lot of object oriented languages, a dictionary would in fact be a class, while an array would be completely different:
public class person //This is a class called "person"
{
     public string name; //A string value called 'name'
     public int age; //An integer value called "age"
     public string food;  //A string value called "food"
     public person() //A constructor for the class, will return an object encapsulating all the data
     {
          name = "John";
          age = 5555;
          food = "pizza";
     }
     /* Now, if a "person" class is constructed with the above constructor, say a "person" class names testPerson, then testPerson.name will equal John, testPerson.age will equal 5555, and testPerson.food will equal pizza. */
}
public class school //A class called "school"
{
     public string[] textBookTitles; //This is what an array looks like in most object-oriented languages. This is an array of strings called "textBookTitles".
}

So as you can see, it's quite a bit different in other languages. And the above class / array structures is from C#, and is practically identical to Java, and very similar (As far as I know) to C++ and C.
astro-mechanic
Scratcher
500+ posts

Dictionaries

Old topic, but they're actually called hashtables. Or key/value pair arrays.

I would like to see these, but they can be worked around pretty easily;

define index [key]
set [i v] to [1]
repeat until <(item (i) of [keys v]) = (key)>
change [i v] by (1)
end

define getValue [key]
index (key)
set [result v] to (item (i) of [values v])

define setValue [key] [value]
index (key)
replace item (i) of [values v] with (value)

define deleteValue [key]
index (key)
delete (i) of [values v]
delete (i) of [keys v]

define addValue [key] [value]
add (key) to [keys v]
add (value) to [values v]
sudoyum
Scratcher
5 posts

Dictionaries

AonymousGuy wrote:

In a lot of object oriented languages, a dictionary would in fact be a class, while an array would be completely different:

I'm not saying dictionaries (or classes) are the same as arrays, I'm just saying dictionaries and classes are different.

astro-mechanic wrote:

Old topic, but they're actually called hashtables. Or key/value pair arrays.

In Ruby, they're called hashtables.

I'm just saying, do not change lists at all. Just add classes or dictionaries or hashtables or key/value pairs of some sort. This is not an extension of the list (array). This is a whole different data type. Lists remain the same and are indexed by numbers.

These classes or dictionaries or whatever are going to be a whole different block color. The Scratch Team gets to decide what they're called and what color they are. You could definitely suggest names, and my suggestion is “dictionary”. They're all the same and represent the same thing: A list of keys and values.




davidkt
Scratcher
1000+ posts

Dictionaries

In Python they're called dictionaries, in Ruby they're called hashtables, and key/value pair arrays is the definition.

(Why does everyone hate Python? I actually wrote an essay for school on how Python is better than Java )
astro-mechanic
Scratcher
500+ posts

Dictionaries

davidkt wrote:

In Python they're called dictionaries, in Ruby they're called hashtables, and key/value pair arrays is the definition.

(Why does everyone hate Python? I actually wrote an essay for school on how Python is better than Java )

“Dictionaries” to me is an alias of “key/value pair array”, “hashtable” refers to the method of implementation; why just add another word if you can use the proper one?

Languages are subjective. I don't hate Python. I like Python. But, I like Java more (really what's surprising to me is how many people hate Java). /offtopic
Chainmanner
Scratcher
100+ posts

Dictionaries

We've already got the “List Contains” block, and if I search for something I'd prefer if it was done from top to bottom, not going straight to it. Sorry, but no support.
sudoyum
Scratcher
5 posts

Dictionaries

Chainmanner wrote:

We've already got the “List Contains” block, and if I search for something I'd prefer if it was done from top to bottom, not going straight to it. Sorry, but no support.

How does the
<[list v] contains [thing]>
block have to do with key/value pair arrays?

List contains could not be used for this purpose:

suppose there are cloud versions of the KEY/VALUE pair arrays.

A user plays your game. You check if the username is a key of the cloud key/value pair array. If it does, you retrieve the user's high score from the previous time he played from the key/value pair array. If it doesn't, You add the username to the key/value pair array and add the value at the end.

Scripts:

when green flag clicked
if <[usernames v] contains key (username)>
set [highscore v] to (value of key (username))
else
add key (username) value (0)
end
big_coconut
Scratcher
13 posts

Dictionaries

define setdefault(key)(value)
if <[keys v] contains (key) ?> then
replace item (item # of (key) in [keys v] :: list) of [values v] with (value)

else
add (key) to [keys v]
add (value) to [values v]

end



define get(key)
set [value v] to (item(item # of (key) in [keys v] :: list) of [values v])



define clear
delete [all v] of [keys v]
delete [all v] of [values v]





This would be so useful

Last edited by big_coconut (June 30, 2021 23:58:54)

Powered by DjangoBB