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

#1 2008-12-30 22:10:30

AlanProjects
Scratcher
Registered: 2008-06-23
Posts: 500+

How do you swap two elements in an array?

I tried this:
[blocks]
<set{ swap }to( item( <{ n }> ) of array>
<replace item( <{ n }> ) of array with item( (( item(<{ n }>) of array> <+> 1 )) )>
<replace item( (( item( <{ n }> ) of array> <+> 1 )) )>
[/blocks]
But it didn't work. Any ideas?

Last edited by AlanProjects (2008-12-30 22:11:47)

Offline

 

#2 2008-12-30 22:40:49

coolstuff
Community Moderator
Registered: 2008-03-06
Posts: 1000+

Re: How do you swap two elements in an array?

set [tempitem] to (item [1] of [array])
replace item [1] of [array] with (item [2] of array)
replace item [2] of [array] with (tempitem)

Try that. I tested it and it works. Just change the ones and twos with whatever numbers of the array you want.

Offline

 

#3 2008-12-30 22:54:56

AlanProjects
Scratcher
Registered: 2008-06-23
Posts: 500+

Re: How do you swap two elements in an array?

I know it works i just made a dumb error.

Offline

 

Board footer