Some questions about arrays.

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
cogollo
Posts: 6
Joined: Wed Nov 19, 2008 8:49 pm

Some questions about arrays.

Post by cogollo » Wed Nov 19, 2008 9:24 pm

Hello, I'm new using RunRev, and I have some questions.

1) How do I create an array from 1 to 20 using a loop instruction, not manually? I need an array like this: [1,2,3,4,...,19,20]

2) How do I select a random value from the array?

3) How do I delete an element from the array?

4) Is there any variable that contains the array's lenght? Or do I need to create a function?

Sorry if these are basic questions for you, but I need help.

BvG
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1239
Joined: Sat Apr 08, 2006 1:10 pm
Contact:

Post by BvG » Wed Nov 19, 2008 10:43 pm

1. creating numerical keys:

Code: Select all

on mouseUp
  repeat with x = 1 to 10
    put "something" into theArray[x]
  end repeat
  put the keys of theArray
end mouseUp
2. Selecting random value:

Code: Select all

put theArray[any line of the keys of theArray]
3. Deleting a key:

Code: Select all

delete variable theArray["1"]
4. No, there is no single line to get the length of all the values in an array (and in rev there normally is no need anyway). You could of course loop trough each line of the keys, and put the length of the contents:

Code: Select all

put 0 into theSum
repeat for each key theKey of theArray
  add the length of theArray[theKey] to theSum
end repeat
put theSum
Various teststacks and stuff:
http://bjoernke.com

Chat with other RunRev developers:
chat.freenode.net:6666 #livecode

SparkOut
Posts: 2943
Joined: Sun Sep 23, 2007 4:58 pm

Post by SparkOut » Wed Nov 19, 2008 10:54 pm

For the array "length" are you talking about the number of keys (or indices)?
If you're used to arrays being of the numeric index type, such as
theArray[0], theArray[1], theArray[2] then you should be aware that in Rev you can use any value, numeric or text as the key (or index).

If you are used to having the length of theArray being returned as 3 in the 3 index example above, then you can get the "length" by returning "the number of lines in the keys of theArray"

cogollo
Posts: 6
Joined: Wed Nov 19, 2008 8:49 pm

Post by cogollo » Thu Nov 20, 2008 12:33 am

Thank you BvG and SparkOut, I'll try your solutions.
For the array "length" are you talking about the number of keys (or indices)?
Yes, that's it, the total number of keys (os values). RunRev script language is a bit different from the classic languages, and it's hard at the beginning.

Thank you again.

cogollo
Posts: 6
Joined: Wed Nov 19, 2008 8:49 pm

Post by cogollo » Thu Nov 20, 2008 2:16 am

Sorry for the double posting.

I have this script in the main card:

Code: Select all

local state, elem, arrayA, arrayB

on preOpenCard
  startProgram
end preOpenCard

on startProgram

   put 0 into state
   
   repeat with x = 1 to 20
      put the value of x into arrayA[x]
   end repeat
   
   repeat with x = 1 to 5
      put arrayA[any line of the keys of arrayA] into elem
      put elem into arrayB[x]
      delete variable arrayA[elem]
   end repeat

end startProgram
I need to populate the "arrayA" with 20 elements (numbers from 1 to 20). Then, I wanna randomly choose 5 elements from arrayA and put'em into arrayB (and delete them from arrayA).

Also I need to show the arrayA into a text box...

What's wrong? So many things, I know... help!

Janschenkel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 977
Joined: Sat Apr 08, 2006 7:47 am
Contact:

Post by Janschenkel » Thu Nov 20, 2008 6:50 am

If you want to 'delete' a variable or an element in an array, you have to use the right key, not the value it contained - in Revolution, there are no pointer or reference variables.

Code: Select all

local state, elem, arrayA, arrayB 

on preOpenCard 
  startProgram 
end preOpenCard 

on startProgram 

   put 0 into state 
    
   repeat with x = 1 to 20 
      put the value of x into arrayA[x] 
   end repeat 
    
   repeat with x = 1 to 5 
      put any line of the keys of arrayA into elemKey
      put arrayA[elemKey] into arrayB[x] 
      delete variable arrayA[elemKey] 
   end repeat 

end startProgram
Also, if you're using only numeric keys in your arrays, you can use the 'extents' property to figure out the dimensions.

Code: Select all

on mouseUp
  local tArray, tRow, tColumn
  -- create a 10x5 matrix array
  repeat with tRow = 1 to 10
    repeat with tColumn = 1 to 5
      put tRow,tColumn into tArray[tRow,tColumn]
    end repeat
  end repeat
  answer the extents of tArray
end mouseUp
HTH,

Jan Schenkel.
Quartam Reports & PDF Library for LiveCode
www.quartam.com

cogollo
Posts: 6
Joined: Wed Nov 19, 2008 8:49 pm

Post by cogollo » Thu Nov 20, 2008 5:38 pm

Thank you very much Jan, but it doesn't work, I'm doing something wrong... How do I watch the array's content?

And I have another question. I have imported an image as a control, and now I need to change the image transparency when I click on it. How do I do that?

gyroscope
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 404
Joined: Tue Jan 08, 2008 3:44 pm
Contact:

Post by gyroscope » Sat Nov 22, 2008 1:27 am

Hi cogollo, i'm still slightly confused with arrays in Revolution (they're a bit different from my "array experience" when I used to use Lingo)...the more experienced guys'll help more, I'm sure.

To change an image transparency when clicked on is relatively straightforward:

Code: Select all

on mouseDown
        set the blendlevel of me to 50
---for instance, where 0 is fully opaque and 100 is fully transparent---
end mouseDown
and a nice effect you might want to try is to fade the image to the transparency required (or fade to nothing):

Code: Select all

on mouseDown
   repeat with BlendOut = 0 to 100 step 2
        set the blendlevel of me to BlendOut
        wait 10 milliseconds
    end repeat
end mouseDown
Hope that's useful for you.

:)

cogollo
Posts: 6
Joined: Wed Nov 19, 2008 8:49 pm

Post by cogollo » Sat Nov 22, 2008 3:34 pm

Thank you gyroscope, the fade effect is beautiful. I have been trying things for a long time and now I understand the arrays: any variable can act like an array. My program is working now, thanks to all who helped me, and sorry about my English writting.

See ya!

SparkOut
Posts: 2943
Joined: Sun Sep 23, 2007 4:58 pm

Post by SparkOut » Sat Nov 22, 2008 4:13 pm

There is an article by Tom Healy on the Rev newsletter issue 57 http://www.runrev.com/newsletter/septem ... etter1.php which should also help you regarding arrays.

Post Reply