Go Back   kirupaForum > Flash > ActionScript 1.0/2.0

Reply
 
Thread Tools Display Modes
Old 10-29-2009, 10:11 PM   #1
SumYungGai
Horrible Actionscripter
 
SumYungGai's Avatar
Location The middle of frickin nowhere

Posts 75
Cleaning empty array elements

So I have an array that when traced, lets say looks like
ActionScript Code:
Carrot,Apple,Strawberry,,,Cantaloupe

I'm just wondering, what's the best way to tell if an element in an array is empty?

__________________
www.ExperimentalFlash.com - Crappy games, crappy toons, and other crappy stuff. Take a few minutes out of your crappy day to go there .
SumYungGai is offline   Reply With Quote

Sponsored Links (Guests Only) - Register | Need Help?
 

Old 10-29-2009, 10:47 PM   #2
Krilnon
≈ ≠ =
 
Krilnon's Avatar
Location Rochester, NY

Posts 7,183
ActionScript Code:
var a:Array = [1, null, undefined, {}.a, 3, 0/0, Infinity, -Infinity, 5];

for(var i:Number = 0; i < a.length; i++){
    if(a[i] == undefined){
        trace('a[' + i + '] (' + a[i] + ') is unstrictly equal to undefined');
    }
}
Krilnon is offline   Reply With Quote
Old 10-30-2009, 07:50 AM   #3
Swooter
Swooter - AS/JS/HTML/CSS
 
Swooter's Avatar
And to remove the empty ones:
ActionScript Code:
var a:Array = ["apple", null, "banana", "", "orange", "", undefined, "grape"];

for ( var i:Number = 0; i < a.length; i++ ) {
    if ( !a[i] ) a.splice(i, 1);
}

trace(a.join(", "));

__________________

AS / JS / HTML / CSS / PHP
Swooter is offline   Reply With Quote
Old 10-30-2009, 11:47 AM   #4
glosrfc
Registered User
 
glosrfc's Avatar
Location Halley Research Station, Latitude 75°35' S, Longitude 26°39' W, Brunt Ice Shelf, Coats Land, Antarctica

Posts 4,158
I'd suggest a slight amendment, just to take care of those pesky consecutive occurences of empty elements, e.g. "apple", null, null, "banana", "", "", "", "orange", "", undefined, "grape":
ActionScript Code:
a.splice(i, 1);
i--;

__________________
©2006 GlosRFC - Searching 8,168,684,336 brain cells
glosrfc is offline   Reply With Quote
Old 10-30-2009, 11:53 AM   #5
senocular
If you can read this, I'm
 
senocular's Avatar
Location San Francisco, CA (USA)

Posts 17,266
^ agreed. Don't splice and walk forward through arrays. You'll skip indices.

__________________
senocular is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 01:45 PM.

SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple. flash components
Creative web apps. Make your own free flash banners and photo slideshows.
Check out the great, high-quality flash extensions. Buy or sell stock flash, video, audio and fonts for as little as 50 cents at FlashDen.

Flash Transition Effects

Flash Effect Tutorials

Digicrafts Components
Flash effects. Art without coding. Upload, publish, deliver. Secure hosting for your professional or academic video, presentations & more. Screencast.com
Streamsolutions Content Delivery Networks Flipping Book - page flip flash component.
Flash-Gallery.com - Get your flash photo gallery (flash component or swf gallery Learn how to advertise on kirupa.com
 

cdn
content delivery network (cdn)

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd. Copyright 2010 - kirupa.com Copyright 2010 - kirupa.com