Log In  

I'm having problems with the del function; its behavior doesn't seem to match what the documentation says it should do. From the docs: (pico-8.txt)

del  t [v]

    Delete the first instance of value v in table t
    [...]
    When v is not given, the last element in the table is removed.
    del returns the deleted item, or returns no value when nothing was deleted.

So, calling del(arr) should be equivalent to calling deli(arr,#arr), right? Both should pop the last element off of arr as far as I understand. But when you run the cartridge attached to this post, the O button works but the X button doesn't

P#80038 2020-07-28 17:35

hmm, I thought I attached a cart but I'm not exactly sure how this forum works and I guess it didn't take. The cartridge is completely empty (no sprites etc) except for the code section:

function _init()
 arr={10,20,30,40,50} 
 color(7)
 col_timer=0
end

function _update()
 if btnp(❎) then
  popped=del(arr)
  color(8)
  col_timer=5
 end
 if btnp(🅾️) then
  popped=deli(arr,#arr)
  color(11)
  col_timer=5
 end
 if col_timer>0 then
  col_timer-=1
  if (col_timer==0) color(7)
 end
end

function _draw()
 cls()
 print("pico-8 version: "..stat(5))
 print("popped: "..tostr(popped))
 print("array: ")
 for i=1,#arr do
  print(" "..i..": "..arr[i])
 end
end
P#80039 2020-07-28 17:37

(oh, also that program prints "pico-8 version: 29" for me, and on bootup my console says its "pico-8 0.2.1b")

P#80040 2020-07-28 17:37

I'd say del(arr,#arr) deletes the first value that is equal to the length of the array.

t={3,1,2}
del(t,#t)
-- t= {1,2}
P#80042 2020-07-28 17:55 ( Edited 2020-07-28 17:56)

true! but I'm talking about two different functions, del and deli, not just del :)

P#80045 2020-07-28 19:47

yeah this looks like a bug to me; del(t) does not act like a pop off the end of the array like the current manual claims.

I think this was added very recently, so it makes sense that maybe the functionality accidentally did not actually make it into the release

P#80047 2020-07-28 20:08 ( Edited 2020-07-28 20:09)

Just to bump this thread because I ran into the same problem today, here's a screenshot of a very simplistic example of the bug:


It's clear that del(a) neither returned anything nor affected the table.

P#85022 2020-12-03 22:28
3

This is a documentation bug.

When zep added deli(), he set it up so omitting the index served as a pop, but didn't do that for del() because the existing behavior of popping nil was to do nothing, which simplifies a lot of cleanup code, where you might be be trying to delete an entry that didn't quite get made, or was an optional component or something like that. He couldn't change it to match the new deli() function's bonus pop behavior because it would almost certainly break many carts.

If you want to pop, use deli(table).

Basically, @zep needs to fix the docs not to say this about del().

P#85185 2020-12-09 06:23 ( Edited 2020-12-09 06:28)

yeah the manual just bit me too.

honestly it wouldn't hurt to deprecate all this. have something easier to wrap your head around like put/get in front, push/pop in back, ins/rem at index, no default parameter (do nothing). some people are actually learning to code here, lists and stacks are essential and this arcane api is confusing. (just my 2cents)

P#87140 2021-02-02 23:59

this was fixed in v0.2.2; hooray! thank you!

(the documentation was changed to match the behavior; it now says:

    del  t v

        Delete the first instance of value v in table t
        [...]
        del returns the deleted item, or returns no value when nothing was deleted.
P#87689 2021-02-15 20:15

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-03-29 12:52:53 | 0.009s | Q:25