Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 08-13-2007, 03:57 PM   #1
handycam
Registered User
 
Join Date: Jul 2007
Posts: 70
Default Make an Array be proper English?

Here's my problem: I have a bunch of arrays ranging from 2 to 8 members. They look, of course, like this:

"red,green,blue"

but I need to wind up with a string of :

"red, green, and blue"

with a space after each comma and an "and" before the last member.

Anyone have a good idea on how to do this programmatically, in a was that I could process any such array from 2 up to 8-10 members? I'm figuring some sort of loop, but my earliest attempts can get complex and I'm looking for simple.

Thanks.
handycam is offline   Reply With Quote
Old 08-13-2007, 04:04 PM   #2
Jim Freer
Senior Member
 
Join Date: Jul 2007
Location: Houston, TX
Posts: 355
Send a message via Yahoo to Jim Freer
Default

Insert " and " in the last element (off a copy of the array if necessary) and use the array toString function.
Jim Freer is offline   Reply With Quote
Old 08-13-2007, 04:21 PM   #3
Slowburn
[Invent:Design:Create]
 
Join Date: Sep 2002
Location: Toronto, ON
Posts: 2,068
Default

this is a quick method of doing what you want. This could probably be better written.

ActionScript Code:
var arr:Array = new Array( "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"); function arrToEnglishString( arr:Array ):String {     var txt:String = arr.join( ", " );     var bgn:String = txt.substring( txt.lastIndexOf( ", " ), 0 );     var end:String = txt.substr( txt.lastIndexOf( ", " ) + 2 );     return( bgn += ", and " + end ); } trace( arrToEnglishString( arr ) );
__________________
o Please do a search before you post
o Please use the [ as][ /as] tag for your code (so we can read it clearly)
Slowburn is offline   Reply With Quote
Old 08-13-2007, 04:25 PM   #4
senocular
six eyes
 
senocular's Avatar
 
Join Date: Jan 2003
Location: San Francisco, CA (USA)
Posts: 7,875
Send a message via ICQ to senocular Send a message via AIM to senocular Send a message via MSN to senocular Send a message via Yahoo to senocular
Default

return arr.slice(0, -1).join(", ") + ", and "+arr[arr.length-1];

should do it
__________________
(6)
senocular is offline   Reply With Quote
Old 08-13-2007, 04:27 PM   #5
Slowburn
[Invent:Design:Create]
 
Join Date: Sep 2002
Location: Toronto, ON
Posts: 2,068
Default

yep, that would do it. nice.
__________________
o Please do a search before you post
o Please use the [ as][ /as] tag for your code (so we can read it clearly)
Slowburn is offline   Reply With Quote
Old 08-13-2007, 05:41 PM   #6
handycam
Registered User
 
Join Date: Jul 2007
Posts: 70
Default

Sweet. Thanks!
handycam is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I make my new array = length of nodes in XML? orlando ActionScript 2.0 2 12-14-2006 05:32 PM
how can i make an array with my own object class siko ActionScript 2.0 0 09-29-2006 04:17 PM
Creating Multidimensional Array jtbrown57 Server-Side Scripting 0 10-22-2005 09:43 PM
ToolTip component bugs hobbis Components 0 09-22-2005 01:45 PM
howto make 3-dimensional array? spyny ActionScript 1.0 (and below) 8 02-01-2004 05:03 AM


All times are GMT. The time now is 04:02 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.