[BASIC language] Removing an array element -- how?

In summary: BASIC. rem initial array size, nrem index of array element to delete, Kfor j=K to n-1 let array$(j)=array$(j+1)rem The loop variable is **j**, not i, so the next line should throw an error.next irem But the array still contains the same number of elements!print "Array size is now ";n-1rem Changing n is not going to help.let n=n-1
  • #1
symbolipoint
Homework Helper
Education Advisor
Gold Member
7,283
1,769
TL;DR Summary
How is an array element to be removed, and put the array back together but smaller by one element?
I cannot figure how to do this. I looked through some old programming textbook and tried an online search and still cannot find. How can one element from an array be removed, and then the array put together but now with the one item less, and so the array is now one item smaller than it was before?
 
Technology news on Phys.org
  • #2
Is this in Algol, or in PL1 :wink: ?
Or are you talking Excel ?:) ?

##\ ##
 
  • Haha
Likes Vanadium 50
  • #3
BvU said:
Is this in Algol, or in PL1 :wink: ?
Or are you talking Excel ?:) ?

##\ ##
No. None of those. BASIC. A flow-diagram would help if possible.
 
  • #4
symbolipoint said:
Summary: How is an array element to be removed, and put the array back together but smaller by one element?

I cannot figure how to do this. I looked through some old programming textbook and tried an online search and still cannot find. How can one element from an array be removed, and then the array put together but now with the one item less, and so the array is now one item smaller than it was before?

Move each item beyond the item you wish to remove down one slot. Then reduce the length of the array by 1.
 
  • Like
Likes OmCheeto
  • #5
symbolipoint said:
None of those. BASIC.
It may depend on the dialect of BASIC.
First, declare a dynamic array with the keyword; ReDim.
Then later you can change the size of the array with; ReDim Preserve.
 
  • #6
Baluncore said:
It may depend on the dialect of BASIC.
Yes: as of 2022 are there any currently supported dialects of BASIC (or of Visual Basic, which is not the same thing at all)?

In Visual Basic there are lists (a subclass of collections) which are more suited to inserting and deleting elements than arrays, which have a fixed length.
 
  • #8
pasmith said:
Move each item beyond the item you wish to remove down one slot. Then reduce the length of the array by 1.
That may be one of the most important keys to figure a way. I sense that I may need to use two arrays to accomplish. One for holding the initial array and other to hold the adjusted array. Still I am confused; maybe slightly less so , but confused.
 
  • #9
Typically, this is not something you would do. Even though is possible; there is a LOT of overhead involved. Arrays are great precisely because they are compact and don't consume a lot of memory; but they are not very flexible.
If you have a program where you frequently need to dynamically change the size of the structure where you are saving data you should be using a list or similar; not an array.
 
  • Like
Likes Vanadium 50
  • #10
SOLVED! I HAVE A METHOD.

The brief advice by @pasmith was helpful but I still needed more effort and some searching .

The method worked this way:
Code:
 rem initial array size, n
rem index of array element to delete, K

for j=K to n-1
    let array$(j)=array$(j+1)
next j
print "Array size is now ";n-1
let n=n-1

EDIT: the "next " code line at the end of the loop was given the wrong variable. Now I have remedied this code posting sample.
 
Last edited:
  • Like
Likes OmCheeto
  • #11
f95toli said:
If you have a program where you frequently need to dynamically change the size of the structure where you are saving data you should be using a list or similar; not an array.
I agree.
But BASIC.
 
  • #12
symbolipoint said:
Code:
 rem initial array size, n
rem index of array element to delete, K

for j=K to n-1
    let array$(j)=array$(j+1)
next i
print "Array size is now ";n-1
let n=n-1

The array isn’t really smaller.
It’s up to you to manage its intended size and manage access and attempts at illegal access.
 
  • #13
symbolipoint said:
The method worked this way:
Really?
Code:
rem initial array size, n
rem index of array element to delete, K

rem Note that arrays in most dialects of BASIC (and Basic) are zero based
rem so if an array contains n elements the index of the last element is n - 1.
for j=K to n-1
    let array$(j)=array$(j+1)
rem The loop variable is **j**, not i, so the next line should throw an error.
next i
rem But the array still contains the same number of elements!
print "Array size is now ";n-1
rem Changing n is not going to help.
let n=n-1

Please say what dialect of BASIC (or Basic) this is, don't make us guess.
 
  • Like
Likes robphy
  • #14
robphy said:
The array isn’t really smaller.
It’s up to you to manage its intended size and manage access and attempts at illegal access.
I guess the last element is still there and needs to also be eliminated from the array. If or when I use the code format in a program, there is still further cleaning work to do. Up to now, I have been confusing the size of the array with the count of elements the array is holding.
 
  • #15
What exactly are you trying to do?

It looks like you are trying to emulate some other data structure using arrays. BASIC being BASIC, you may have no choice, but it is probable that you will end up with a mess. This is also a sign that you are trying to push BASIC past its intended scope.
 
  • #16
Vanadium 50 said:
What exactly are you trying to do?

It looks like you are trying to emulate some other data structure using arrays. BASIC being BASIC, you may have no choice, but it is probable that you will end up with a mess. This is also a sign that you are trying to push BASIC past its intended scope.
I have only been able to learn BASIC. I tried a few times to learn other languages but with no meaningful success of any kind. My formal knowledge of any "data structures" is either severely limited or nonexistent. I can make some very simple programs using BASIC. I had been able to work with numeric and string variables, do a few things with arrays; use loops, use decision structures; do a few simple things with sequential files. There is a simple program I recently began to create, but soon enough found that I need to relearn some skills that I lost; so I have been making a small number of small programs to relearn what I have lost skill for. What I recently "began to create" is a type of record-keeping program, and made some good progress. Then, I came to the part of the design at which I wanted a user to be able to delete a record (which, in the program is managed in a two-dimensional array), and I found I was stuck.
 
  • #17
symbolipoint said:
I have only been able to learn BASIC. I tried a few times to learn other languages but with no meaningful success of any kind. My formal knowledge of any "data structures" is either severely limited or nonexistent. ...
You do not need to change from BASIC.
Everything you need is available in the later dialects, such as MS Visual Basic, or FreeBASIC which is free. FreeBASIC has a help forum where they will almost write your code for you, while explaining what you need to know.
https://en.wikipedia.org/wiki/FreeBASIC
https://www.freebasic.net
 
  • Like
Likes symbolipoint
  • #18
This are, of course, different languages that are called BASIC.
 
  • #19
symbolipoint said:
wanted a user to be able to delete a record
Ah! Now we're getting somewhere.

You have your array, perhaps called DATA(N). You have a second array of booleans (if your version of BASIC supports them, otherwise you can emulate them with 1's and 0's or T's and F's) ACTIVE(N). To delete record #M, set ACTIVE(M) to FALSE (or 0, or F). Your FOR loops then have immediately following them an IF statement testing that ACTIVE for that records is TRUE.

When you write the data out - you are going to write it out, aren't you? - if you only write the elements that are still ACTIVE you will get a nice contiguous array when you read it back in.
 
  • Like
Likes 256bits
  • #20
pbuk said:
Really?
Code:
rem initial array size, n
rem index of array element to delete, K

rem Note that arrays in most dialects of BASIC (and Basic) are zero based
rem so if an array contains n elements the index of the last element is n - 1.
for j=K to n-1
    let array$(j)=array$(j+1)
rem The loop variable is **j**, not i, so the next line should throw an error.
next i
rem But the array still contains the same number of elements!
print "Array size is now ";n-1
rem Changing n is not going to help.
let n=n-1

Please say what dialect of BASIC (or Basic) this is, don't make us guess.
I see what you are saying now. I first recopied the section of code which I posted, and made some readjustments and did a bit of faulty editing. Yes. I wrote in the wrong variable, and should have shown, "next j". I will return to the post I made, and adjust it.
 
  • #21
Your version of BASIC may be a really old one that does not have dynamically-sized arrays. The FORTRAN 77 that I used from the late 1970s through the 1980s was like this. When you declared an array as

REAL ARRAY(10)

you got an array of ten items (floating-point numbers in this case), indexed from 1 through 10. If you wanted indexes from 0 through 9, you had to declare REAL ARRAY (0:9). Either way, you couldn't change the size or index range afterwards.

If you wanted the array to (effectively) contain a variable number of items, you had to use a separate variable, maybe called N, to keep track of the index of the last item, and ignore any items beyond it.

But woe would befall you if you let N, or any array index, exceed 10 for my first declaration, or 9 for my second declaration. The compilers I used would cheerfully let you "walk off the end of the array" and thereby read from or write to memory locations belonging to other variables or arrays, or even to FORMAT statements which specified the layout of the output from your PRINT or WRITE statements. Inadvertently changing your FORMAT statements while the program was running could lead to utterly baffling output, if the program didn't simply crash with an output error. :eek:
 
  • Wow
Likes Wrichik Basu
  • #22
jtbell, post #21,
What the BASIC I use allows is , if the possibility could be needed, to use a REDIM statement to change the number of elements that an array could hold. I do not know if this is the same as a "dynamically sized" array.
 
  • #23
Vanadium 50 said:
This are, of course, different languages that are called BASIC.
Yes: I understand the OP is using what is sometimes called a "second generation" basic which does not support a non-destructive
Code:
REDIM
and so the only way to end up with an array with the correct number of entries is to create a new one and copy it over. Alternatively use the "flag" technique described in #19.

But these techniques are 30 years old, there is no point relearning them in 2022.
 
  • #24
symbolipoint said:
jtbell, post #21,
What the BASIC I use allows is , if the possibility could be needed, to use a REDIM statement to change the number of elements that an array could hold. I do not know if this is the same as a "dynamically sized" array.
I think you will find that REDIM in the BASIC you are using destroys the contents of the array.
 
  • #25
pbuk said:
I think you will find that REDIM in the BASIC you are using destroys the contents of the array.
Unless you use the PRESERVE keyword to preserve the data.
 
  • #26
Baluncore said:
Unless you use the PRESERVE keyword to preserve the data.
Not available in Liberty BASIC, which is what the OP is using.
 
  • #27
pbuk said:
Not available in Liberty BASIC, which is what the OP is using.
How did you identify it was Liberty BASIC ?
 
  • #28
Theer are hundreds of dialects of BASIC out there. If it absolutely has to be BASIC, picking one with more database-like features is best. If it has to be one particular dialect, the OP should tell us which one it has to be.
 
  • Like
Likes robphy
  • #29
pbuk said:
so the only way to end up with an array with the correct number of entries is to create a new one and copy it over. Alternatively use the "flag" technique described in #19.

But these techniques are 30 years old, there is no point relearning them in 2022.
they may be 30 years old techniques but that is what goes on under the hood in a good many languages .
C++ vector has a vector::resize () function that copies the old vector of size n into a new vector of size nn, and deletes the old vector. To the programmer, one thinks that the old vector has positions added to it, but since continuous memory is needed that just ain't so.

It should be the same for REDIM in BASIC also, which would necessarily use continuous memory for the array.

Not sure if one can delete unused memory in any Basic version? such as for an array that does not need its use anymore.
 
  • #30
256bits said:
C++ vector has a vector::resize () function that copies the old vector of size n into a new vector of size nn, and deletes the old vector. To the programmer, one thinks that the old vector has positions added to it, but since continuous memory is needed that just ain't so.
Yes, and the OP has been advised to use a language which has richer data structures than the contiguous array (although I wouldn't choose C++ over, say, Python).

256bits said:
It should be the same for REDIM in BASIC also, which would necessarily use continuous memory for the array.
As has already been mentioned this works if the you can use REDIM PRESERVE, but as has also already been mentioned this is not available in the OP's dialect of BASIC.

256bits said:
Not sure if one can delete unused memory in any Basic version? such as for an array that does not need its use anymore.
Yes, a more simple example is reassigning a (longer) value to a string variable. Whether you can make use of this memory or not depends on the sophistication of the garbage collector used in the specific implementation of the specific dialect. It's 2022, nobody can be bothered with thinking about this any more: even Microsoft are no longer interested in Visual Basic .NET.
 
  • Like
Likes 256bits
  • #31
Liberty BASIC appears to be missing much of the structured data that is in VB and FB.
Being limited to a 2D array makes it more of a teaching language than a productive workhorse. $60 seems a high price to pay for restricted features, when it can be fully featured, open source, and free.

But BASIC is quite capable of the task. I would start with a record structure for the file storage, then write routines to read and write that to an array, with a validity flag to make deletion easy. Reading the file into the array, editing, then writing it back PRESERVEs data, and eliminates deleted data. REDIM the dynamic array to fit the size of the data file it must hold. For data entered by hand, cheap memory and disc storage are not a limit. The limit is your ability to structure the data record.
 
  • #32
Baluncore said:
But BASIC is quite capable of the task. I would start with a record structure for the file storage, then write routines to read and write that to an array, with a validity flag to make deletion easy. Reading the file into the array, editing, then writing it back PRESERVEs data, and eliminates deleted data. REDIM the dynamic array to fit the size of the data file it must hold. For data entered by hand, cheap memory and disc storage are not a limit. The limit is your ability to structure the data record.
Much of what you characterize there is what I have spent the last two or three weeks relearning. (and I have not finished this relearning.)
 
  • #33
256bits said:
Not sure if one can delete unused memory in any Basic version? such as for an array that does not need its use anymore.
REDIM Array( 0 [,0] ) releases the memory.
Memory is released when an array or variable is lost from scope.
If you can do something in C, you can do it in FreeBASIC, because FB is now a frontend that translates to C, links to libraries, optimises and compiles to various platforms.
 
  • Like
Likes 256bits

What is an array and how is it used in BASIC language?

An array is a data structure that can store multiple values of the same data type in a single variable. In BASIC language, arrays are used to store and manipulate large sets of data efficiently.

How do I remove an element from an array in BASIC language?

To remove an element from an array in BASIC language, you can use the REMOVE command followed by the index of the element you want to remove. For example, REMOVE myArray(3) will remove the element at index 3 from the array named myArray.

Can I remove multiple elements from an array at once in BASIC language?

Yes, you can remove multiple elements from an array at once in BASIC language by using the REMOVE command with a range of indices. For example, REMOVE myArray(2 to 5) will remove elements at indices 2, 3, 4, and 5 from the array named myArray.

What happens to the remaining elements in the array after removing an element?

After removing an element from an array in BASIC language, the remaining elements will shift to fill the empty space. For example, if you remove an element at index 3, the element at index 4 will become the new element at index 3, and so on.

Is there a way to remove an element from an array without shifting the remaining elements?

Yes, you can use the REMOVEAT command in BASIC language to remove an element from an array without shifting the remaining elements. This command takes two arguments - the array name and the index of the element to be removed. For example, REMOVEAT myArray, 3 will remove the element at index 3 without shifting the remaining elements.

Similar threads

  • Programming and Computer Science
Replies
20
Views
1K
  • Programming and Computer Science
7
Replies
235
Views
10K
  • Programming and Computer Science
Replies
23
Views
1K
  • Engineering and Comp Sci Homework Help
2
Replies
43
Views
4K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
2
Replies
47
Views
4K
  • Programming and Computer Science
2
Replies
66
Views
4K
  • Programming and Computer Science
Replies
8
Views
879
  • Programming and Computer Science
12
Replies
397
Views
13K
  • Programming and Computer Science
Replies
5
Views
2K
Back
Top