This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2013-03-27 05:11:17

Imaginos
New Scratcher
Registered: 2013-03-06
Posts: 56

Arrays?

I'm sorry if this seems a foolish question but here goes...

Back in the old days (80s) whenn I used to dabble in BASIC on my ZX Spectrum, a much used programming technique was to define up an array of variables. Is this possible within Scratch?

Offline

 

#2 2013-03-27 06:35:17

shadowmouse
New Scratcher
Registered: 2013-02-03
Posts: 100+

Re: Arrays?

I read something about this a while ago. I know in scratch that you can add variables to lists (which are scrtach's version of arrays) and in BYOB you can put lists in lists. Hope that helps.

Offline

 

#3 2013-03-27 06:40:09

Imaginos
New Scratcher
Registered: 2013-03-06
Posts: 56

Re: Arrays?

Thanks shadowmouse. I presumed that Lists were the Scratch version of arrays but I couldn't see any information on Lists in the Scratch Reference Guide.

Offline

 

#4 2013-03-27 15:14:59

ErnieParke
Scratcher
Registered: 2010-12-03
Posts: 1000+

Re: Arrays?

Imaginos wrote:

Thanks shadowmouse. I presumed that Lists were the Scratch version of arrays but I couldn't see any information on Lists in the Scratch Reference Guide.

Well your presumption is in a sense correct because lists are arrays, just 2D or with only one column, whichever way you like to look at it.

Anyway, you want to form a 3D array, correct?

With regards,

ErnieParke


http://i46.tinypic.com/35ismmc.png

Offline

 

#5 2013-03-28 03:16:45

Imaginos
New Scratcher
Registered: 2013-03-06
Posts: 56

Re: Arrays?

Yes Ernie, that's what I was thinking of... to define a grid of backgrounds (stages) etc...

Offline

 

#6 2013-03-29 21:07:23

ErnieParke
Scratcher
Registered: 2010-12-03
Posts: 1000+

Re: Arrays?

Imaginos wrote:

Yes Ernie, that's what I was thinking of... to define a grid of backgrounds (stages) etc...

Oh, okay. This shouldn't be too hard, with the right technique. That's because Scratch doesn't have a native system to create arrays. So this raises a question; how are we going to create an array?

So to make a 3D array (it's actually 2D, though it acts as a 3D one), all you have to do is create a list, set the first two items to the array's length and width, and you set the length of the list to your length * width + 2. Now, reading left to right, up to down in your imaginary array, number the first item 3, the second 4, and on... Their number will now correspond to their position in the list. Now you have an array!

Just as an example, let's say we want to turn the following 2x2 array into it's corresponding list:

12    6

4     15


After a bit of editing, we get:

2 //Length
2 //Width
12
6
4
15


Does this make sense?

Now, the final thing you need to know is how to read an item from the list. If you know its column and row, just use this:

(item (((((Row) -(1)) * (length)) + (column)) + (2)) of [Array v])

I hope that this helps!

With regards,

ErnieParke

Last edited by ErnieParke (2013-03-29 21:08:33)


http://i46.tinypic.com/35ismmc.png

Offline

 

#7 2013-03-29 23:15:41

bob6
Scratcher
Registered: 2010-07-01
Posts: 100+

Re: Arrays?

I agree with what Ernie says, but alternatively, you can set up constants for the length and width, to prevent any silly coding mistakes and clean up the code a little bit (having an extra +2 in the code makes it just that much less user-friendly)

wink


http://i46.tinypic.com/3148ksz.gif

Offline

 

Board footer