Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tables
#1
Quick question about tables...

I know that the "insert" command can be used to add stuff to the table (insert tbl, index, value), but I cannot find a command to "remove" stuff from a table. I figured that the "insert" help (F1) would possibly provide either reference or a link to removing... I figured wrong... *sigh*

Can anyone provide a method of removal?

J
Logic is the beginning of wisdom.

Live long and prosper.
Reply
#2
Code:
free_key.n7
free key tbl, key

' free key
' --------

' Create an array, print its size and elements.
a = [13, 14, 15, 16, 17]
pln "Size of a = " + sizeof(a)
for i = 0 to sizeof(a) - 1 pln " a[" + i + "] = " + a[i]

' 'free key t, k' removes the entry with key k from table t if it exists. If the key is numeric, any
' sequent numeric keys will be decreased.
free key a, 2
' Print again.
pln "Size of a after removing index 2 = " + sizeof(a)
for i = 0 to sizeof(a) - 1 pln " a[" + i + "] = " + a[i]

' Do the same thing but with string keys.
a = [foo: 10, bar: 20, baz: 30]
pln "Size of a = " + sizeof(a)
foreach k, v in a pln " a." + k + " = " + v

free key a, "baz"
pln "Size of a after removing key baz = " + sizeof(a)
foreach k, v in a pln " a." + k + " = " + v

system "pause"

Code:
free_val.n7
free val tbl, value

' free val
' --------

' Put some numbers and strings in an array and print its size and elements.
a = [13, "hello", 2, "boop", "hello", 13, 4]
pln "Size of a = " + sizeof(a)
for i = 0 to sizeof(a) - 1 pln " a[" + i + "] = " + a[i]

' 'free val t, v' removes every entry with v as value from table t. If entries with numeric keys are
' removed from a sequence, any sequent keys will be re-indexed.
free val a, "hello"
free val a, 2
' Print again.
pln "Size of a after removing value hello and 2 = " + sizeof(a)
for i = 0 to sizeof(a) - 1 pln " a[" + i + "] = " + a[i]

' Do the same thing but with string keys.
a = [foo: "hello", bar: 2, baz: "hello", qux: 25]
pln "Size of a = " + sizeof(a)
foreach k, v in a pln " a." + k + " = " + v

free val a, "hello"
free val a, 2
pln "Size of a after removing value hello and 2 = " + sizeof(a)
foreach k, v in a pln " a." + k + " = " + v

system "pause"
Reply
#3
Yep!

free key my_array, 3

, removes index 3 from my_array and re-indexes (index 4 becomes 3, and so on).

free val my_array, 3

, removes all occurrences of value 3 from my_array and re-indexes.
Reply
#4
Thanks, guys! Much appreciated!
Logic is the beginning of wisdom.

Live long and prosper.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)