tables problem

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

tables problem

Mike Atamas
Hi,
I am having a problem with tables. The culprit is the following function:

function dep:remove( _name )
   for i = 1, table.getn(self.deps), 1
   do
       if self.deps[i]["name"] == _name
       then
            table.remove(self.deps, i)
       end
   end
end

When I call this function I get the following error:

lua: depends.lua:22: attempt to index field `?' (a nil value)
stack traceback:
       depends.lua:22: in function `remove'
       depends.lua:37: in main chunk
       [C]: ?

I checked and i is a valid element in that table. This function does seem to work when i is the last element of the table. I also tried typing a number in directly. When I replaced table.remove(self.deps, i) with table.remove(self.deps, 1) things also failed. Then I tried taking it out of the for loop, and everything worked.

Can someone help me fix this?

Mike Atamas
[hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: tables problem

D Burgess-2
May or may not be your problem but,
from the manual -

All three control expressions are evaluated only once,
before the loop starts.

Note the once.

After the first table.remove, the second parameter of
the 'for' will be a number larger than the number of 
table entries.
+++++++++++++++++++++++++++++++++
>Hi,
>    I am having a problem with tables. The culprit is the following 
>function:
>
>function dep:remove( _name )
>    for i = 1, table.getn(self.deps), 1
>    do
>        if self.deps[i]["name"] == _name
>        then
>             table.remove(self.deps, i)
>        end
>    end
>end
>
>When I call this function I get the following error:
>
>lua: depends.lua:22: attempt to index field `?' (a nil value)
>stack traceback:
>        depends.lua:22: in function `remove'
>        depends.lua:37: in main chunk
>        [C]: ?
>
>I checked and i is a valid element in that table. This function does 
>seem to work when i is the last element of the table. I also tried 
>typing a number in directly. When I replaced table.remove(self.deps, i) 
>with table.remove(self.deps, 1) things also failed. Then I tried taking 
>it out of the for loop, and everything worked.
>
>Can someone help me fix this?
>
>Mike Atamas
>[hidden email]
			





Reply | Threaded
Open this post in threaded view
|

Re: tables problem

Mike Atamas
I changed it and got things to work. Here is what the function looks like now:

function dep:remove( _name )
   for i = 1, table.getn(self.deps), 1
   do
       if self.deps[i]["name"] == _name
       then
           a = i
       end
   end
   table.remove(self.deps,a)
end

Thanks for the help

D Burgess wrote:

May or may not be your problem but,
from the manual -

All three control expressions are evaluated only once,
before the loop starts.

Note the once.

After the first table.remove, the second parameter of
the 'for' will be a number larger than the number of table entries.
+++++++++++++++++++++++++++++++++
Hi,
I am having a problem with tables. The culprit is the following function:

function dep:remove( _name )
  for i = 1, table.getn(self.deps), 1
  do
      if self.deps[i]["name"] == _name
      then
           table.remove(self.deps, i)
      end
  end
end

When I call this function I get the following error:

lua: depends.lua:22: attempt to index field `?' (a nil value)
stack traceback:
      depends.lua:22: in function `remove'
      depends.lua:37: in main chunk
      [C]: ?

I checked and i is a valid element in that table. This function does seem to work when i is the last element of the table. I also tried typing a number in directly. When I replaced table.remove(self.deps, i) with table.remove(self.deps, 1) things also failed. Then I tried taking it out of the for loop, and everything worked.

Can someone help me fix this?

Mike Atamas
[hidden email]
			









Reply | Threaded
Open this post in threaded view
|

Re: tables problem

Eric Tetz-2
--- Mike Atamas wrote:
> I changed it and got things to work. Here is what the function
> looks like now:

A couple suggestions: make 'a' local and don't bother checking
remaining array elements after you've found the one you're
looking for:
 
function dep:remove( _name )
    local a ---> ADDED
    for i = 1, table.getn(self.deps)
    do
        if self.deps[i]["name"] == _name
        then
            a = i
            break ---> ADDED
        end
    end
    table.remove(self.deps,a)
end


__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free web site building tool. Try it!
http://webhosting.yahoo.com/ps/sb/

Reply | Threaded
Open this post in threaded view
|

Re: tables problem

Frank Meier-Dörnberg
In reply to this post by D Burgess-2
Mike,
 
 You can change the loop direction and it works too !
 
 ...
 for i = table.getn(self.deps),1,-1 do
     if self.deps[i]["name"] == _name then
        table.remove(self.deps, i)
     end
 end
 ...
 
 Frank

Reply | Threaded
Open this post in threaded view
|

Re: tables problem

andyfirebir
In reply to this post by Eric Tetz-2
>A couple suggestions: make 'a' local [...]


Call me stupid - or to be more accurate, call me a Lua 4 programmer - but do you actually need the local statement in Lua 5?

In Lua 4, sure.  But I thought that we were "lexically scoped" now.  Doesn't that mean that a is automatically local to the function?  Or have I misunderstood?

Andy.


Reply | Threaded
Open this post in threaded view
|

RE: tables problem

Virgil Smith
>In Lua 4, sure.  But I thought that we were "lexically scoped" now.
> Doesn't that mean that a is automatically local to the function?
> Or have I misunderstood?
>Andy.

You've misunderstood.  You still need the local statement to make a local
variable.  Because of "lexical scoping" you have a lot more options on how
local a "local" is, but Lua still defaults to global unless you use the
local statement.  For instance...


Foo = 1
Bar = function() do
    local Foo = 5

    do
        local Foo = 7 -- I <think> this is illegal in Lua 4
        return Foo
    end
end

contains 3 separate Foo's, while ...

Foo = 1
Bar = function() do
    Foo = 5

    do
        Foo = 7
        return Foo
    end
end

contains only 1 global Foo.



Reply | Threaded
Open this post in threaded view
|

Re: tables problem

Wim Couwenberg-5
In reply to this post by andyfirebir
> but do you actually need the local statement in Lua 5?
> In Lua 4, sure.  But I thought that we were "lexically scoped" now.
> Doesn't that mean that a is automatically local to the function?

No. Name lookup is still te same as in Lua 4: a variable name is searched
outward (in nested scopes) and upward (in script.) If it is not found in a
local statement or as a function argument (including implicit `self' and
`arg') then it is assumed to be a global, i.e. it will be looked up in the
function's environment.

What lexical scoping added compared to Lua 4 is:
 - upvalues do no longer need to be declared in the inner most enclosing
scope,
 - upvalues are mutable,
 - no `%' syntax anymore (yay!),
 - no global upvalues anymore (declare them local first).

So the `a' in the example would still be global if not explicitly declared
local.

--
Wim


Reply | Threaded
Open this post in threaded view
|

RE: tables problem

andyfirebir
In reply to this post by Virgil Smith
>Foo = 1
>Bar = function() do
>    local Foo = 5
>
>    do
>        local Foo = 7 -- I <think> this is illegal in Lua 4
>        return Foo
>    end
>end

Not only is it not illegal, it works just as well.   Consider my slightly changed version of your program:

Foo = 1
function Bar()
    local Foo = 5
    do
        local Foo = 7 
        print( Foo )
    end
    print( Foo )
end
Bar()
print( Foo )

[1017]~: /usr/bin/lua -f luatest
7
5
1

...three foos in Lua 4 as well.  Which means I *really* have to go back and read the manual again...


Reply | Threaded
Open this post in threaded view
|

RE: tables problem

Virgil Smith
My apologies to Andy and all.
I <thought> I knew what I was talking about.

Here are links to the relevant spec sections...

http://www.lua.org/manual/4.0/manual.html#4.6

http://www.lua.org/manual/5.0/manual.html#visibility


Responder (i.e. me in this case) RTFM yourself.





-----Original Message-----
From: [hidden email]
[[hidden email] Behalf Of
[hidden email]
Sent: Tuesday, January 27, 2004 2:31 PM
To: Lua list
Subject: RE: tables problem



>Foo = 1
>Bar = function() do
>    local Foo = 5
>
>    do
>        local Foo = 7 -- I <think> this is illegal in Lua 4
>        return Foo
>    end
>end

Not only is it not illegal, it works just as well.   Consider my slightly
changed version of your program:

Foo = 1
function Bar()
    local Foo = 5
    do
        local Foo = 7
        print( Foo )
    end
    print( Foo )
end
Bar()
print( Foo )

[1017]~: /usr/bin/lua -f luatest
7
5
1

...three foos in Lua 4 as well.  Which means I *really* have to go back and
read the manual again...




Reply | Threaded
Open this post in threaded view
|

RE: tables problem

andyfirebir
>My apologies to Andy and all.

It's cool.    I'm learning, you're learning - that's what makes it fun.