Dimensional Arrays

Ken Ray kray at sonsothunder.com
Wed Mar 14 09:54:56 EDT 2007


On Wed, 14 Mar 2007 13:03:08 +0200, Nic Prioleau wrote:

> I appologise in advance for what may seem like simple programming 
> logic to some, but I'm not really a programmer and I've been staring 
> at this problem for 5 hours now and can't seem to figure it out. I 
> have 3 multi-dimensional arrays which I need to get "grouped" 
> information out of:-
> 
> tArray1["Data",1,"regNo"] = "B123ABC"
> tArray1["Data,1,"tLitres"] = "500"
> tArray1["Data",1,"month"] = 2
> tArray1["Data",2,"regNo"] = "B123ABC"
> tArray1["Data,2,"tLitres"] = "300"
> tArray1["Data",2,"month"] = 3
> tArray1["Data",3,"regNo"] = "B456ABC"
> tArray1["Data,3,"tLitres"] = "200"
> tArray1["Data",3,"month"] = 4
> 
> tArray2["Data",1,"regNo"] = "B123ABC"
> tArray2["Data",1,"tLitres"] = "1000"
> tArray2["Data",1,"tKM"] = "2500"
> tArray2["Data",1,"month"] = 2
> tArray2["Data",2,"regNo"] = "B123ABC"
> tArray2["Data,2,"tLitres"] = "150"
> tArray2["Data",2,"tKM"] = "3000"
> tArray2["Data",2,"month"] = 3
> tArray2["Data",3,"regNo"] = "B456ABC"
> tArray2["Data,3,"tLitres"] = "150"
> tArray2["Data",3,"tKM"] = "3000"
> tArray2["Data",3,"month"] = 4
> 
> AND....
> tArray3["Data",1,"regNo"] = "B123ABC"
> tArray3["Data",1,"tLitres"] = "350"
> tArray3["Data",1,"tKM"] = "800"
> tArray3["Data",1,"month"] = 2
> tArray3["Data",2,"regNo"] = "B123ABC"
> tArray3["Data,2,"tLitres"] = "250"
> tArray3["Data",2,"tKM"] = "600"
> tArray3["Data",2,"month"] = 3
> tArray3["Data",3,"regNo"] = "B456ABC"
> tArray3["Data,3,"tLitres"] = "250"
> tArray3["Data",3,"tKM"] = "100"
> tArray3["Data",3,"month"] = 4
> 
> what I need to end up with is data from these 3 arrays which will end 
> up looking something like this:
> 
> regNo "B123ABC" used 1850Ltrs and did 3300 km in month 2
> regNo "B123ABC" used 700Ltrs and did 3600 km in month 3
> regNo "B456ABC" used 500Ltrs and did 3100 km in month 4
> 
> Can anyone explain how I might be able to do this. I basically got 
> the info by doing 3 'grouped' select statements from 3 different 
> tables. This info gets stored in the arrays above but when it comes 
> time to writing a report or doing calculations, I need to add them 
> all up. It's probably staring me in the face but I have 'arc eye' 
> from staring at the screen for so long!

No problem, although the structure of the arrays you have is a little 
odd (although it works fine). That is, if the key of an array has 
multiple quoted strings in it, it "collapses" to a single quoted 
string. For example:

   tArray3["Data",3,"month"]=4

is the same as:

   tArray3["Data,3,month"]=4

Honestly, I didn't know you could have multiple quoted strings in a key 
and have it work (learn something new every day!). However to grab your 
data you could do this:

-- If you have a fixed set of 3 arrays which have the same keys:
put "B123ABC" into tRegNo
put "2" into tMonth
put "" into tReport
repeat for each line tKey in (the keys of tArray1)
  if item 3 of tKey is "month" then
    put tArray1[tKey] into tMonth
    put item 2 of tKey into tRecNo  -- this is the record number (I'm 
assuming)
    put tArray1["Data," & tRecNo & ",regNo"] into tRegNo
    
    put tArray1["Data," & tRecNo & ",tKM"] into tKM1
    put tArray2["Data," & tRecNo & ",tKM"] into tKM2
    put tArray3["Data," & tRecNo & ",tKM"] into tKM3
    put tArray1["Data," & tRecNo & ",tLitres"] into tLitres1
    put tArray2["Data," & tRecNo & ",tLitres"] into tLitres2
    put tArray3["Data," & tRecNo & ",tLitres"] into tLitres3
    put "regNo" && quote & tRegNo & quote && "used" && \
       (tLitres1+tLitres2+tLitres3) & "Ltrs and did" && \
       (tKM1+tKM2+tKM3) && "km in month" && tMonth & \
       CR after tReport
end repeat
delete char -1 of tReport -- removes trailing CR
put tReport into field 1

HTH,

Ken Ray
Sons of Thunder Software, Inc.
Email: kray at sonsothunder.com
Web Site: http://www.sonsothunder.com/



More information about the use-livecode mailing list