• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Delete unused spaces in an Array

Community Beginner ,
Sep 22, 2017 Sep 22, 2017

Copy link to clipboard

Copied

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

Current.jpeg

Wanted Result

wanted.jpeg

TOPICS
ActionScript

Views

951

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Sep 23, 2017 Sep 23, 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);

}

}

}

Votes

Translate

Translate
LEGEND ,
Sep 22, 2017 Sep 22, 2017

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 22, 2017 Sep 22, 2017

Copy link to clipboard

Copied

A text input box.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 22, 2017 Sep 22, 2017

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 22, 2017 Sep 22, 2017

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 22, 2017 Sep 22, 2017

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 22, 2017 Sep 22, 2017

Copy link to clipboard

Copied

It looks hard to read! But I took this line: "where users can fill inn different textboxes" to mean that a human would enter the text into that input field.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 22, 2017 Sep 22, 2017

Copy link to clipboard

Copied

Having someone input the index of an array to which data will be assigned is asking for empty space to be created... which could lead to commas all over the place depending on how the data is being fed into the text file.

var arr:Array = new Array();

arr[20] = "stuff";

trace(arr);  // yields    ,,,,,,,,,,,,,,,,,,,,stuff

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 23, 2017 Sep 23, 2017

Copy link to clipboard

Copied

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" );

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 23, 2017 Sep 23, 2017

Copy link to clipboard

Copied

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);

}

}

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 23, 2017 Sep 23, 2017

Copy link to clipboard

Copied

a.length-1?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 24, 2017 Sep 24, 2017

Copy link to clipboard

Copied

yes, thank you.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 26, 2017 Sep 26, 2017

Copy link to clipboard

Copied

kglad  skrev

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);

}

}

}

I added your function and it worked. On my mac it looks perfect!

Ferdig MAC.jpeg

Allthough on my windows machine when i open the same .txt file it looks like this

Ferdig Windows.PNG

Any idea why?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 26, 2017 Sep 26, 2017

Copy link to clipboard

Copied

LATEST

the arrays will look the same before and after compressF. there's probably a difference in the way the text file is created in win vs mac.  if you want to add a line break after each array element when writing to a text file on win, add that ('\r').

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 26, 2017 Sep 26, 2017

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines