lua-users home
lua-l archive

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


I'd also like 'table.append()' for appending numerically indexed tables
together and 'table.flatten()' for removing any subtables (placing their
items in main level).

But -as someone pointed out- there's probably so much variation in needs
that it's best to leave these for each developer's hands.

Anyways, here's the code that I'm using for table content-wise equality. In
case it helps.

-----
-- bool= Loc_CoveringContents( tbl1, tbl2 )
--
-- Returns 'true' if 'tbl1' covers all entries of 'tbl2' (with identical
contents).
-- This means that 'tbl2' is a subset of 'tbl1' entries.
--
local function Loc_CoveringContents( tbl1, tbl2 )
    --
    for k,v in tbl2 do
        --
        if (tbl1[k] ~= v) then
            
            if ((type(tbl1[k])~="table") or (type(v)~="table")) then
                --
                return false    -- some entry didn't exist or was different!
            end
            
            -- Subtables need to be dived into (different refs doesn't mean
            -- different contents).
            --
            if (not Loc_CoveringContents( tbl1[k], v )) then
                return false
            end
            
            -- go on...
        end
    end    
    
    return true     -- covered it all!
end


-----
-- bool= Loc_IdenticalContents( tbl1, tbl2 )
--
-- Returns 'true' if the two tables are content-wise identical, 'false' if
not.
--
local function Loc_IdenticalContents( tbl1, tbl2 )
    --
    return (Loc_CoveringContents( tbl1, tbl2 ) and
            Loc_CoveringContents( tbl2, tbl1 ))
end


Edelleenlähetetyn viestin alku:

> Lähettäjä: "jimmyp_gr" <jimmyp@hal.csd.auth.gr>
> Päiväys: sunnuntai, 13. huhtikuuta 2003  03:19:43 +0300
> Vastaanottaja: Multiple recipients of list <lua-l@tecgraf.puc-rio.br>
> Aihe: table equality
> Vastaus: lua-l@tecgraf.puc-rio.br
>
> Since tables are comapered 'by reference' (something I agree with) it
> would be handy to have a function wich would compare two tables 'by
> value'. I searched the archives and didn't come up with any
> requests/questions on this subject, wich I though was kind of strange.
> Anyway I'm sure there are complications wich escape me right now but
> how come there's nothing like that in the table library?
>
> Dimitris
>
>
###########################################
This message has been scanned by F-Secure Anti-Virus for Microsoft Exchange.