lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I might be way off here, and this might almost certainly be clumsy 
compared to anyone else's answer here, but the core of it might help 
you. The function is a direct paste from a script of mine, with stuff 
you probably won't want, but it shows how I solved a 2D problem with 
seperate parallel arrays instead of storing several inside one. It 
effectively links each parallel entry in the horizontal axis as if 
the vertical arrays were associative, with one being indexed to 
reference the row. While my code here was just to add, edit, delete, 
one 'row', you could also use it to sort rows according to a chosen 
column, like in many GUI apps, and you could of course sort a single 
array seperately.


-- Arrays are P and V, in this case, parameter, value..
-- IN1 and IN2 are search string inputs, and EDIT is a flag for read/
write actions.

function ListEdit(P,IN1,V,IN2,EDIT)  local I=0
    repeat  I=I+1  until (P[I]==nil or IN2==V[I])
    if (P[I] and (IN1~=P[I] and EDIT=="+") or (IN1==P[I] and EDIT=="-"))
 then  tremove(P,I)  tremove(V,I)  end
    if (IN1~=P[I] and EDIT=="+") then  tinsert(P,IN1)  tinsert(V,IN2) 
 end
end


I hijacked this snippet from somewhere, well, reduced it a bit from 
what I found, but never used it. It's a sorting thing. As far as I 
remember, it was stable, not interfering with the order of equal 
entries, but I never used it so I didn't explore deeply. Anyway:

--    if (P[1]) then  for X=1,N-1 do  for Y=1,N-X do
--                if strupper(LIST[Y])>strupper(LIST[Y+1]) then  LIST
[Y],LIST[Y+1]=LIST[Y+1],LIST[Y]  end
--    end end end



John A GILBERT <johng@arandasys.com> wrote:
(26/08/2004 17:00)

>
>Friends,
>
>This is a very basic programming query, but I just can't get it worked-out.
>Suppose that there were a 2-d array ( the contents of a simple spreadsheet for example) and one wished to sort a column, say:
>could the table.sort instruction be applied to such an array were it to be defined in the form Array [ x ][y ]?
>
>Any help would be very much appreciated.
>Thank you
>
>John Gilbert.
>(Lua used in scripting an accounting program )
>
>-- 
>John A GILBERT
>Aranda Systems Limited,
>11,Seafield Crescent,
>Blackrock,        tel.: +353-(0)1-260 09 63
>Co DUBLIN,        fax.:           260 09 64
>Ireland.          mob.: +353-(0)86-065 60 55
>Publishers of Linear Transactions Processor© 
>Ireland
>www.arandasys.com