delete

Prev Next

Function Names

delete

Description

This function deletes the specified variable including all members if existing. If an array member is deleted, (e.g. delete( a[2] );), then the remaining array members further down close up. Obviously, members of structures can also be deleted.

Attention: If the function call is executed in a local varaible context (e.g inside a user-defined fuction or a B4P program called by another B4P program using start() or include(), then attempting to delete a non-local variable asserts an exception. You can circumvent this by deleting the global variable in a global context provided with the global() function.
example: global() delete ( a global variable [ ] );

Call as: procedure

Restrictions

Indirect parameter passing is disabled

Parameter count

Min. 1

Parameters

No.TypeDescription
1, ...
code
variable
:string
Variable name

The affected variable will be deleted resp. their members deleted.

Exceptions

Variable is protected
Attempting to delete global variable in local context
Attempting to delete system variables

Examples

  values[] = { 0, 11, 22, 33, 44, 55, 66, 77, 88, 99 };
  a[] = This is the base variable;

  array( a[], values[] );
  delete( a[4], a[2] ); // Deletes subscript 2 and 4.  Good idea to work from bottom to top

  for all variables( a[], v[] ) print( v[], " " ); echo ("Note: 22 and 44 are missing. OK.");

  echo("Attention when deleting lower subscripts before upper ones");
  array( a[], values[] );
  delete( a[2], a[4] ); // Deletes subscript 2 and 5 (not 4, becuase member variables have shifted up)
  for all variables( a[], v[] ) print( v[], " " ); echo ("Note: 22 and 55 are missing this time");

  echo;
  echo("And with structures:", new line);

  structure( a[], { Cat,   Dog,  Bird,  { Parrot, Parakeet  }, Rat,  Mouse, Elephant, Zebra },
          { Katze, Hund, Vogel, { Papagei, Sittich  }, Ratte, Maus, Elefant,  Zebra } );

  delete( a[Zebra]);
  delete( a[Bird], a[Elephant], a[Cat] );
  delete( a[Dinosaur] );

  // B4P issues no error message if attempting to delete nonexisting variables,
  // assuming they have already been deleted

  see(a[]);               // Dog Rat Mouse

  delete(a[] );
  echo( "Variable a[] existing? ", existing(a[]) );

Output

0 11 33 55 66 77 88 99 Note: 22 and 44 are missing. OK.
Attention when deleting lower subscripts before upper ones
0 11 33 44 66 77 88 99 Note: 22 and 55 are missing this time

And with structures:

a[]                     This is the base variable    (softquoted string,full access)
Dog                     Hund                       (softquoted string,full access)
Mouse                   Maus                       (softquoted string,full access)
Rat                     Ratte                      (softquoted string,full access)

Variable a[] existing? false
Try it yourself: Open LIB_Function_delete.b4p in B4P_Examples.zip. Decompress before use.

See also

release