How to make a matrix AxB using CRPL?

Started by pawel345, January 29, 2014, 09:23:19 AM

Previous topic - Next topic

knucracker

Yeah, lists in lists work... any of the data types can be held by a list, which includes other lists.  
You can create a jagged array using just that technique.  In a jagged array the rows don't have to all be the same length.

So the jagged array might look like this (6 rows tall with variable row width):
x x x x x
x x x
x x x x
x x x x x x x
x
x x x x  

All of this falls under the 'data structure' category and it's something you learn in the first couple years of a computer science (or related) degree.  That said, the practical application of data structures isn't rocket science so anybody can learn the basic structures and terminology whenever they choose.  Start here:
http://en.wikipedia.org/wiki/Data_structure

Just knowing the possible structures and some example use cases can take your coding skills up several notches.

Clean0nion

Would this work?

$arrayWidth:0
$arrayHeight:0

once

<-arrayHeight 0 do
  CreateList "h" I concat ->!
  I ->i
loop

<-i ->hlength

<-hlength 0 do
  <-arrayWidth 0 do
    "h" J concat <-! I AppendToList
  loop
loop

endonce

Flabort

Sorry, I think something's wrong with that. The "i" variable seems to be set in the first loop seems to come out equal to "arrayHeight", and then you transfer the SAME value to "hlength", and when you call it again, you could just use arrayHeight again instead.

$arrayWidth:0
$arrayHeight:0

once

<-arrayHeight 0 do
  CreateList "h" I concat ->!
loop

<-arrayHeight 0 do
  <-arrayWidth 0 do
    "h" J concat <-! I AppendToList
  loop
loop

endonce

That said, the code seems useless as is, it creates, Y lists with X variables equal to their position in the list. Without some way to utilize those lists... but of course, that would be what goes OUTSIDE the onceblock.
My maps: Top scores: Sugarplum, Cryz Dal, Cryz Torri, Cryz Bohz (Click fetch scores, page courtesy of kwinse)

Clean0nion

It should be <-arrayHeight 1 sub. So that's pretty much what it is.
No, wait - you're right. Yes.

And I did begin making the utilisation tool, then I remembered I had no idea what I was doing, or even what you want to extract from this list.

Clean0nion

So I expanded on my previous code and made this:

$img:"Custom0_256"
$size:4
$dist:10
$arrayWidth:100
$arrayHeight:100

   CreateList ->hh
   <-arrayHeight 0 do
      CreateList "h" I concat ->!
      <-hh "h" I concat AppendToList
   loop
   <-hh GetListCount 0 do
      <-arrayWidth 0 do
         "h" J concat <-! I AppendToList
      loop
   loop
   0 ->hadd
   <-arrayHeight 0 do
      <-arrayWidth 0 do
         Self <-hh J GetListElement I GetListElement <-img SetImage
         Self <-hh J GetListElement I GetListElement <-size <-size SetImageScale
         Self <-hh J GetListElement I GetListElement I 8 mul <-dist mul J 8 mul <-dist mul 3 SetImagePosition
      loop
   loop


What it's supposed to do is add an array of images to a core so that the collection of images covers a large square, with the image directly over the core being at the bottom-left. It's all in the initial once loop of the script.

What does it do? Nothing. Doesn't even set a single image. I have no idea what I'm doing wrong (other than possibly all of it) - can anyone help?

eduran

I have no idea what you are trying to do with all these lists. If there is a purpose to them that isn't visible I'd need to know what that is before I can help you figure out what's wrong with them.
Without the lists, this should do to set the images:
<-arrayHeight 0 do
<-arrayWidth 0 do
"x" i "y" j concat concat concat ->imgSlot
Self <-imgSlot <-img SetImage
Self <-imgSlot <-size <-size SetImageScale
Self <-imgSlot I 8 mul <-dist mul J 8 mul <-dist mul 3 SetImagePosition
loop
loop



Clean0nion

Ah, yes.
What I'm trying to do is convert a number in the array to a set of coords, which is the opposite of what you were doing.
So 3 might convert to 0,3 or 3,0 depending on how I ordered the J and I.

Grayzzur

Storing lists in lists is interesting, and the <-! and ->! commands are too. They allow for complicated logic. Most of the time though, they are unnecessary. Just because you can use something, doesn't mean you should. Take a step back and look at what you're actually trying to do.

If I'm interpreting your intent correctly, you don't need to convert a number in an array to a set of coords, all you need to do is find a way to store a grid of images in a single CRPL core and display them on screen in a grid. Yes?

If that's the case, take a look at eduran's sample, or some derivative of handling it that way. The simpler path to achieving your goal will be easier to understand and maintain.

I would not try to maintain a list of lists unless they were dynamic and/or varying in their sizes.

On a side note, in your provided code, you are using J inside a single loop, not a nested loop.

   <-arrayHeight 0 do
      CreateList "h" I concat ->!
      <-hh "h" I concat AppendToList
   loop
   <-hh GetListCount 0 do
      <-arrayWidth 0 do
         "h" J concat <-! I AppendToList ##### J has no practical value here, this loop is not inside another loop *****
      loop
   loop
"Fate. It protects fools, little children, and ships named 'Enterprise.'" -William T. Riker

Clean0nion

#23
Done.

   0 ->i
   0 ->xd
   0 ->yd
   while <-side 2 pow <-i gt
      repeat
         Self "img" <-i concat <-img SetImage
         Self "img" <-i concat <-size dup SetImageScale
         Self "img" <-i concat <-xd 8 mul <-dist mul SetImagePositionX
         Self "img" <-i concat <-yd 8 mul <-dist mul SetImagePositionY
         
         <-xd 1 add ->xd
         
         <-xd <-side mod 0 eq if
            <-yd 1 add ->yd
            0 ->xd
         endif
         
         <-i 1 add ->i
   endwhile


This probably isn't greatly transferable to any other project, but it does exactly what I want.

ZackNAttack

I made a matrix script. :)
Zack on cw3