+ Reply to Thread
Results 1 to 9 of 9

Thread: E2 array and table additions

  1. #1
    Wire Sofaking immibis's Avatar
    Join Date
    Nov 2009
    Location
    Wellington, New Zealand
    Posts
    401

    Default E2 array and table additions

    E2 should have the following functions:
    • R:removeShift(N) - Removes the array entry at that index, shifts all higher indices down by 1
    • R:remove(N) - Removes the array entry at that index, does not affect other indices
    • T:remove(S) - Removes the entry with that index
    • R:contains(N) - Returns 1 if the array contains the number as an index, 0 otherwise.
    • R:containsValue(*) - Returns 1 if the array contains the object as a value, 0 otherwise. (useful but not necessary)
    • T:contains(S) - Returns 1 if the table contains the string as a key, 0 otherwise.
    • T:containsValue(*) - Returns 1 if the table contains the object as a value, 0 otherwise. (probably not very useful)
    • R:getType(N) - Returns the type of the value with index N. (probably not very useful)
    • T:getType(S) - Returns the type of the value with key S. (probably not very useful)

    Currently there's no way to remove entries from tables or arrays (except for R:unshift* and Rop*) and no way to check if a table or array contains a given key/index reliably. (you could use if(Array[N, number]) but what if it contains the index and it's 0? and what if it's not a number?)

    0a3c696d6d696269733e092e2e2e7774662c20776879206973 20746865726520612068696464656e20666f72756d2063616c 6c6564206469783f200a3c4a6174476f6f6477696e3e093e2e 3e200a3c4a6174476f6f6477696e3e093c2e3c200a3c49616d 4d634c6f76696e3e094e4f200a3c49616d4d634c6f76696e3e 094e4f204e4f204e4f204e4f204e4f

  2. #2
    Wiremode Helper Techni's Avatar
    Join Date
    Jul 2008
    Posts
    773
    Blog Entries
    1

    Default Re: E2 array and table additions

    You can do all of these via regular array functions through loops and such, adding them to the LUA wouldn't have any speed increase as you'd still have to search through the entire array/table for most of these.

    R:removeShift(N)
    That's how the current array remove works.

    R:remove(N)
    Not really possible (That's not how arrays work)

    T:remove(S)
    Can be done by setting the string to a null value, ie 0, vec(), noentity() etc

    R:contains(N)
    Requires a linear search through the array, can be done in 3 lines of E2 code and will be laggy with large arrays even in LUA. Use a lookup table

    R:containsValue(*)
    As above

    T:contains(S)
    Use a look up table

    T:containsValue(*)
    use a look up table

    # R:getType(N)
    # T:getType(S)
    Might be useful.

  3. #3
    No u Divran's Avatar
    Join Date
    Jul 2008
    Location
    Sweden
    Posts
    4,582

    Default Re: E2 array and table additions

    Quote Originally Posted by immibis View Post
    Currently there's no way to remove entries from tables or arrays
    Quote Originally Posted by E2 Wiki
    Code:
    R:remove<type>(N) 	 * 	 Deletes and returns the specified entry, moving subsequent entries down to compensate
    R:remove(N) 		Deletes the specified entry, moving subsequent entries down to compensate
    Wire Expression2 - GMod Wiki
    SVN Tutorial
    My SVN:
    Code:
    http://divranspack.googlecode.com/svn/trunk/%20divranspack/
    Get dropbox and get 250 MB extra space: Dropbox

  4. #4
    Wire Sofaking immibis's Avatar
    Join Date
    Nov 2009
    Location
    Wellington, New Zealand
    Posts
    401

    Default Re: E2 array and table additions

    Quote Originally Posted by Techni View Post
    You can do all of these via regular array functions through loops and such, adding them to the LUA wouldn't have any speed increase as you'd still have to search through the entire array/table for most of these.
    No, Lua can currently do more with arrays and tables than E2 can.
    Quote Originally Posted by Techni View Post
    R:removeShift(N)
    That's how the current array remove works.
    Okay.
    Quote Originally Posted by Techni View Post
    R:remove(N)
    Not really possible (That's not how arrays work)
    It is in E2. Again,
    Code:
    Array[Index] = nil
    Quote Originally Posted by Techni View Post
    T:remove(S)
    Can be done by setting the string to a null value, ie 0, vec(), noentity() etc
    If doing that removes it from the table, then E2 tables are messed up.
    Quote Originally Posted by Techni View Post
    R:contains(N)
    Requires a linear search through the array, can be done in 3 lines of E2 code and will be laggy with large arrays even in LUA. Use a lookup table
    What about
    Code:
    return (Array[Index] ~= nil)
    Quote Originally Posted by Techni View Post
    R:containsValue(*)
    As above.
    Yes, this one would require a linear search through the array. It would still be a lot easier and slightly faster than doing the same in E2, though, and could use ops proportional to the size of the table. Or it could be omitted altogether, since it's not particularly useful.
    Quote Originally Posted by Techni View Post
    T:contains(S)
    Use a look up table
    Um, what? The table T is the lookup table. That's why you're trying to look up the key S in it. And again,
    Code:
    return (Table[Key] ~= nil)
    Quote Originally Posted by Techni View Post
    T:containsValue(*)
    use a look up table
    Same response as A:containsValue(*) above

    Disclaimer: I don't know much Lua so the Lua code provided is probably slightly wrong. However, it's also probably mostly right.

    PS. It's Lua not LUA.

    0a3c696d6d696269733e092e2e2e7774662c20776879206973 20746865726520612068696464656e20666f72756d2063616c 6c6564206469783f200a3c4a6174476f6f6477696e3e093e2e 3e200a3c4a6174476f6f6477696e3e093c2e3c200a3c49616d 4d634c6f76696e3e094e4f200a3c49616d4d634c6f76696e3e 094e4f204e4f204e4f204e4f204e4f

  5. #5
    Wire Amateur Teh Reborn Devil's Avatar
    Join Date
    Jun 2010
    Location
    Norway, Larvik
    Posts
    99

    Default Re: E2 array and table additions

    Quote Originally Posted by immibis View Post
    It is in E2. Again,
    Code:
    Array[Index] = nil
    That's the same as the remove function. Array[Index,entity] = noentity() will move all entries above one step down.
    E2 is awesome <3

  6. #6
    No u Divran's Avatar
    Join Date
    Jul 2008
    Location
    Sweden
    Posts
    4,582

    Default Re: E2 array and table additions

    Quote Originally Posted by Teh Reborn Devil View Post
    Array[Index,entity] = noentity() will move all entries above one step down.
    No it won't

    Quote Originally Posted by immibis View Post
    Um, what? The table T is the lookup table. That's why you're trying to look up the key S in it. And again,
    Code:
    return (Table[Key] ~= nil)
    Then what's wrong with the E2 code
    Code:
    if (Table[Key,type]) {
    or similar
    SVN Tutorial
    My SVN:
    Code:
    http://divranspack.googlecode.com/svn/trunk/%20divranspack/
    Get dropbox and get 250 MB extra space: Dropbox

  7. #7
    Wire Sofaking immibis's Avatar
    Join Date
    Nov 2009
    Location
    Wellington, New Zealand
    Posts
    401

    Default Re: E2 array and table additions

    For numbers and strings at least, that returns false if the entry is not present, 0, or "". T:contains(S) would return false only if the entry is not present.

    The way it currently is, I need two lookup tables to do the same thing. One storing the actual data, and another so I can tell whether an entry exists or not.

    You seem to be assuming that having an entry equal to 0/""/noranger()/noentity()/nobone() are the same thing as not having any entry at all... It's not. If you have a table storing string data for a group of objects, for example, then "" means the object exists but there's no data on it, and not-present means the object doesn't exist.

    0a3c696d6d696269733e092e2e2e7774662c20776879206973 20746865726520612068696464656e20666f72756d2063616c 6c6564206469783f200a3c4a6174476f6f6477696e3e093e2e 3e200a3c4a6174476f6f6477696e3e093c2e3c200a3c49616d 4d634c6f76696e3e094e4f200a3c49616d4d634c6f76696e3e 094e4f204e4f204e4f204e4f204e4f

  8. #8
    No u Divran's Avatar
    Join Date
    Jul 2008
    Location
    Sweden
    Posts
    4,582

    Default Re: E2 array and table additions

    Actually, noentity() is the same as not having it at all, because "noentity()" returns "nil". In Lua, setting a table's value to nil removes it. There are a few other data types that do this, but some (like number, string, vector, etc) return their own "nil" values.

    Code:
    e2function entity noentity()
    	return nil
    end
    But yes I see your point
    SVN Tutorial
    My SVN:
    Code:
    http://divranspack.googlecode.com/svn/trunk/%20divranspack/
    Get dropbox and get 250 MB extra space: Dropbox

  9. #9
    Wire Amateur Teh Reborn Devil's Avatar
    Join Date
    Jun 2010
    Location
    Norway, Larvik
    Posts
    99

    Default Re: E2 array and table additions

    Quote Originally Posted by Divran View Post
    No it won't
    Lol, it does that for me when I play online.

    Could be something weird with the servers I play on then.
    E2 is awesome <3

+ Reply to Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
proceed-collector
proceed-collector
proceed-collector
proceed-collector
linguistic-parrots
linguistic-parrots
linguistic-parrots
linguistic-parrots