Skip to main content
Hwerner
Participating Frequently
September 22, 2017
Answered

Delete unused spaces in an Array

  • September 22, 2017
  • 5 replies
  • 1295 views

Hello

im delevoping a program where users can fill inn different textboxes such as username, ID and other information. This info is saved into an array. The array is sorted by what the user puts into the ID field.

function laan(evt:MouseEvent) :void {

   if     (boxDate.text != "") {

        feideId = boxFeide.text

        id = boxId.text

        dato = boxDate.text

        checkBox()             //Function to check if textboxes are empty or not

       if (feideId.length > 7 && feideId.length < 10 && boxId.text != "") {

            tfOutput.text = "Dato(" + dato + ")  " + feideId +" har lånt overgang: " + id + "\n \n"

            laanArray [id] = new Array("\n Dato[" + dato + "] Feide[" + feideId + "] ID[" + id + "]\n")

            clear()                // Function to empty all textboxes

            trace("laanArray " + laanArray)

        }

}

This array then gets saved to a .txt file.

The only problem is if theres unused spaces in the array that .txt file gets "," where the unused spaces in the array are.

Here are pictures that explain better.

Current Result

Wanted Result

This topic has been closed for replies.
Correct answer kglad

you can remove null elements using:

function compressF(a:Array):void{

for(var i:int=a.length-1;i>=0;i--){

if(a==null){

a.splice(i,1);

}

}

}

5 replies

Hwerner
HwernerAuthor
Participating Frequently
September 26, 2017

Sorry for the late response and thank you for the replies

I want to clarify.

I am fairly new to programing and what im trying to make is a register for users that borrow equipment from my office.

All equipment is marked with their own IDs and all the users have  a username.

When users fill in their username and the ID of the item they are borrowing i want the program to save that data to a document.

This i why i have the array put ID as the index

When they deliver the item they borrowed they fill in their username again and the ID of the item they borrowed. I then want the program to find that ID in the array and but a line of text next to it "laanArray.splice(id, 1,"\n Dato[" + dato + "] Feide[" + feideId + "] ID[" + id + "] <- Levert! \n" )"

and here is the function for delivering a borrowed item

function levert(evt:MouseEvent) :void {

    feideId = boxFeide.text

    id = boxId.text

    dato = boxDate.text

    checkBox()      //Function to check if textboxes are empty or not

    if (feideId.length > 7 && feideId.length < 10 && boxId.text != "") {

        tfOutput.text = "Dato(" + dato + ")  " + feideId +" har levert overgang: " + id + "\n \n"

        laanArray.splice(id, 1,"\n Dato[" + dato + "] Feide[" + feideId + "] ID[" + id + "] <- Levert! \n" )

        trace ("laanArray " + laanArray)

        clear()      // Function to empty all textboxes

    }

Now to answer your questions and ask some of my own

1.

What are you using to write the data to the text file?  What sets the value of boxId.text?

boxId is one of the 2 text boxes the user fills in to borrow an item there is one for their username and the other is for the ID of the item. So the user determines the value of boxId.text.

To write data to the text file im using "myfiles.save". Currently this is done by a button you have to press but i would like to get it to autosave whenever a new item is borrowed or delievered.

function save(evt:MouseEvent) :void {

    myfiles.save(laanArray, 'Overgang dokumentasjon.txt')

}

2.

Also, unless you have some reason for it, there is no need to create an array out of each element you plant in the laanArray.  If you didn't realize it, that is what you are doing, creating arrays of one element within the laanArray.

I didnt know, thank you.

3.

boxDate.text is another textbox that the administrator of the program will fill in before users can borrow items.

Again thank you for all the help

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
September 24, 2017

you can remove null elements using:

function compressF(a:Array):void{

for(var i:int=a.length-1;i>=0;i--){

if(a==null){

a.splice(i,1);

}

}

}

Colin Holgate
Inspiring
September 24, 2017

a.length-1?

kglad
Community Expert
Community Expert
September 24, 2017

yes, thank you.

Ned Murphy
Legend
September 23, 2017

If you create an array only to end up deleting empty indices then you probably do not want to create the array that way in the first place.  One option is to look into using an associative array (or something similar) where the keys are not numeric indices but strings.  So your ID values will be strings rather than numbers.

Adobe Flash Platform * Associative arrays

Another option would be to create the array elements as objects, wherein one of the elements of each object is the ID value.  You would use the push function to fill the array instead of using the ID value as the index.

Also, unless you have some reason for it, there is no need to create an array out of each element you plant in the laanArray.  If you didn't realize it, that is what you are doing, creating arrays of one element within the laanArray.

             laanArray [id] = new Array("\n Dato[" + dato + "] Feide[" + feideId + "] ID[" + id + "]\n");

should probably just be...

             laanArray [id] = "\n Dato[" + dato + "] Feide[" + feideId + "] ID[" + id + "]\n";

and what my second suggestion is saying is you could use something like...

             laanArray.push( { idNum: id,  value="\n Dato[" + dato + "] Feide[" + feideId + "] ID[" + id + "]\n" } );

unless the id is really of no meaning, in which case you could just use...

             laanArray.push( "\n Dato[" + dato + "] Feide[" + feideId + "] ID[" + id + "]\n" );

Ned Murphy
Legend
September 22, 2017

That doesn't answer either of the questions I asked.

Colin Holgate
Inspiring
September 22, 2017

He did say textinput, which answers the question of what sets the value of boxld.text.

With input fields you can control what characters are allowed, and can prevent the usr from typing any spaces. See here:

Adobe ActionScript 3.0 * Restricting text input

Ned Murphy
Legend
September 22, 2017

No Colin, it doesn't answer the question of what sets the value.  The textinput is the creature I am asking about - it doesn't set its own value.  Something has to put that value there.  That value is defining the index of an array that is being created for each entry in the parent array

    laanArray [id] = new Array("\n Dato[" + dato + "] Feide[" + feideId + "] ID[" + id + "]\n")

It looks fishy

Ned Murphy
Legend
September 22, 2017

What are you using to write the data to the text file?  What sets the value of boxId.text?

Hwerner
HwernerAuthor
Participating Frequently
September 22, 2017

A text input box.