Discuss Scratch

gatzke
Scratcher
100+ posts

Matrices / 2D arrays?

There are many cases where a 2D array (matrix) of data would make sense.

You can do 2D with a single dimension, but it takes a good bit more effort to do so in your code. Supporting it natively in Scratch would allow for some cool applications.

I put together a matrix package for basic linear algebra, but the code is a mess IMHO because of the requirement to do things with only 1D arrays.

http://scratch.mit.edu/projects/24145965/

Now if someone would just implement HSL Scratch could take over the world…

Last edited by gatzke (March 12, 2015 13:28:45)

MegaApuTurkUltra
Scratcher
1000+ posts

Matrices / 2D arrays?

Why don't you make a fortran emulator?

Anyway, you can emulate 2d lists with 1d lists by using things like
definesetxytovalue note: x and y start at zeroreplaceitemy*listwidth+x+1of2d listwithvalue
gatzke
Scratcher
100+ posts

Matrices / 2D arrays?

I know you can do 2D with 1D, it just adds an extra layer of confusion by not having a native data type.

Maybe the issue is you can't pass a list variable into a subroutine? Is that correct? Just numeric values and strings? Or am I mistaken.

Yes, a FORTRAN or COBOL emulator in Scratch would be a beautiful thing. But only if it runs off punchcards.
MegaApuTurkUltra
Scratcher
1000+ posts

Matrices / 2D arrays?

gatzke wrote:

Maybe the issue is you can't pass a list variable into a subroutine? Is that correct? Just numeric values and strings? Or am I mistaken.
You need some hax, but it can be done
definesetxyoflistNametovalue note: x and y start at zeroreplaceitemy*listwidth+x+1oflistNamewithvalue
Then replace the block spec with
["procDef", "set %n %n of %m.list to %s", ...]
                          ^^^this
Then whenever you call it, you can choose a list from the menu.
setxyofSample Text Listtovalue

See @nXIII's spreadsheet for more stuff you can hack into custom block specs
gatzke
Scratcher
100+ posts

Matrices / 2D arrays?

That is amazing but scary. Not sure where you make that change or how you make that change. In the Scratch text file? It doesn't error check and cut that right out if you are running online?

In my library, I assumed you initialized the size of each matrix, so each matrix had a integer ID starting at 1. That initialization initialized a workspace list, adding a 0 for each element, and added size information to other lists. That way all you needed was the matrix id integer value. All the data is buried in lists that you never have to work with. You can set and retrieve element information from the list.

MegaApuTurkUltra
Scratcher
1000+ posts

Matrices / 2D arrays?

It's not scary at all. Json hacking is easy, just download the project from the editor, open it with an archive manager, open project.json in your favorite text editor, and hack away. Once you're done, upload the project back to the editor.
Zro716
Scratcher
1000+ posts

Matrices / 2D arrays?

MegaApuTurkUltra wrote:

It's not scary at all. Json hacking is easy, just download the project from the editor, open it with an archive manager, open project.json in your favorite text editor, and hack away. Once you're done, upload the project back to the editor.
you don't even need an archive manager:
1. download project as a ZIP file
2. open contents and copy the project.json to your desktop
3. open the file to notepad
4. ctrl+f, “%m” and add .list to the end of them
5. save the file
6. move the file back to the zip folder, replace file in destination
7. change project extension to .sb2
8. profit
MegaApuTurkUltra
Scratcher
1000+ posts

Matrices / 2D arrays?

Zro716 wrote:

MegaApuTurkUltra wrote:

It's not scary at all. Json hacking is easy, just download the project from the editor, open it with an archive manager, open project.json in your favorite text editor, and hack away. Once you're done, upload the project back to the editor.
you don't even need an archive manager:
1. download project as a ZIP file
2. open contents and copy the project.json to your desktop
3. open the file to notepad
4. ctrl+f, “%m” and add .list to the end of them
5. save the file
6. move the file back to the zip folder, replace file in destination
7. change project extension to .sb2
8. profit
3complicated5me. I use 7-zip, which temporarily extracts the files and runs notepad++, then packs the archive automatically once I'm done editing. No need to do more work than necessary.
gatzke
Scratcher
100+ posts

Matrices / 2D arrays?

But if the ST ever decides to parse your projects for completeness or correctness this mod would get your work bounced methinks.

I will try to work within the bounds they have provided for me :-(

JayZX535
Scratcher
62 posts

Matrices / 2D arrays?

I second this– you can kinda replicate it using what scratch has now, but it takes a ton of code and is a pain in the neck. Actual 2-d arrays would be marvelous.
MegaApuTurkUltra
Scratcher
1000+ posts

Matrices / 2D arrays?

gatzke wrote:

But if the ST ever decides to parse your projects for completeness or correctness this mod would get your work bounced methinks.

I will try to work within the bounds they have provided for me :-(

Although they say only projects made with the Scratch editor are allowed to be shared, I don't see them enforcing the rule. Plus, djdolphin is making Scratchception

Last edited by MegaApuTurkUltra (March 14, 2015 16:36:05)

comp09
Scratcher
1000+ posts

Matrices / 2D arrays?

MegaApuTurkUltra wrote:

gatzke wrote:

But if the ST ever decides to parse your projects for completeness or correctness this mod would get your work bounced methinks.

I will try to work within the bounds they have provided for me :-(

Although they say only projects made with the Scratch editor are allowed to be shared, I don't see them enforcing the rule. Plus, djdolphin is making Scratchception
I think you meant this project: https://scratch.mit.edu/projects/50169416/
MegaApuTurkUltra
Scratcher
1000+ posts

Matrices / 2D arrays?

comp09 wrote:

MegaApuTurkUltra wrote:

gatzke wrote:

But if the ST ever decides to parse your projects for completeness or correctness this mod would get your work bounced methinks.

I will try to work within the bounds they have provided for me :-(

Although they say only projects made with the Scratch editor are allowed to be shared, I don't see them enforcing the rule. Plus, djdolphin is making Scratchception
I think you meant this project: https://scratch.mit.edu/projects/50169416/
ooook, thanks again Scratch search for not displaying relevant results on top. I'll fix it
Sorry djdolphin!
Cyoce
Scratcher
500+ posts

Matrices / 2D arrays?

Here's how it should work: accept lists as a data structure! Allow the `item of ` block to accept dropdown lists!
item1oflistinstead ofitem1oflist
Then, you can do:
item1ofitem1oflist`list` is a list of lists, so `item 1 of (list)` returns a list
gatzke
Scratcher
100+ posts

Matrices / 2D arrays?

I have seen that, but I think it leads to some complexity issues and maybe some syntax implementation problems. You give too much freedom and some things become very dangerous and others stop working I think.

0D, 1D, and 2D data types would not be a huge stretch to add.
theonlygusti
Scratcher
1000+ posts

Matrices / 2D arrays?

gatzke wrote:

0D, 1D, and 2D data types would not be a huge stretch to add.
Especially as 2 of those are already within scratch

You don't need a new data type to make multidimensional arrays though, no programming language (of which I k ow) does that: they are all just arrays of arrays.
gatzke
Scratcher
100+ posts

Matrices / 2D arrays?

Correct. Got me.

In MATLAB, everything is an N dimensional array (pretty much).

A 2D array is just pretty darn useful in a lot of cases without opening up the can of works that lists of lists could make.

People get numbers. People get lists of numbers. People get 2D storage of numbers. CS majors get lists of lists.

I am not trying to infer that CS majors are not people.
Rex208
Scratcher
500+ posts

Matrices / 2D arrays?

JayZX535 wrote:

I second this– you can kinda replicate it using what scratch has now, but it takes a ton of code and is a pain in the neck. Actual 2-d arrays would be marvelous.
This.
WeaponGod243
Scratcher
79 posts

Matrices / 2D arrays?

bump of four years

Powered by DjangoBB