How to split a String and add to an ArrayCollection - actionscript-3

I have a string like this:
var tempSting:String = "abc#abc.com;xyz#xyz.com"
I want to add this String into the ArrayCollection. And the above String should be divided by mail id and remove the ; symbol and need to add asArrayCollection
tempAc:Arraycollection = new ArrayCollection{abc#abc.com, xyz#xyz.com}
Please help me to add the split String into the ArrayCollection.

var tempString:String="abc#abc.com;xyz#xyz.com";
var tempArray:Array=tempString.split(";");
//tempAc is a predefined and presumably prepopulated arraycollection
for each(var email:String in tempArray) {
tempAc.addItem(email);
}
EDIT Now that I see Shane's answer I must add the following:
This code will append the array to the arraycollection. If you just want to create a new arraycollection, all you need to do is:
var tempAc:ArrayCollection=new ArrayCollection(tempArray);
or in 1 line,
var tempAc:ArrayCollection=new ArrayCollection(tempString.split(";"));
UPDATE - to answer questions in the comments:
tempAc.getItemAt(i) will give you the email id at the i th position
tempAc.getItemIndex("someone#email.com") will give you the index at which someone#email.com exists in the arraycollection (or -1 if not contained)
tempAc.contains("someone#email.com") will return true or false depending on if the string is contained or not in the arraycollection
So, to check for duplicate ids, all you got to do is:
var newEmailId:String="someone#email.com";
if(!tempAc.contains(newEmailId)) {
tempAc.addItem(newEmailId);
}

var tempString:String = "abc#abc.com;xyz#xyz.com";
tempAC:ArrayCollection = new ArrayCollection(tempString.split(";"));

Related

as3 replacing array with another array but same name

this question should be an easy one, but how do I replace an array or define it and then give it values based off a users choice. The code with the if statements below are some choices I need in one or both arrays.
For example
if(choice=="easy")
{
var sorted:Array = new Array("Beau","Dad","Jesus","Mary","Mom");
}
if(choice=="hard")
{
var sorted:Array = new Array("Beau","Dad","Jesus","Mary","Mom","Jordyn","Presley","Daddy","Mommy","Grandma","Grandpa","Nana","Gepa");
}
But this doesn't work above.
Thanks.
Declare the variable outside the condition instead (I also changed if/if to if/elseif as choice can't be both easy and hard at the same time):
var sorted:Array;
if(choice=="easy")
{
sorted = new Array("Beau","Dad","Jesus","Mary","Mom");
} else if(choice=="hard")
{
sorted = new Array("Beau","Dad","Jesus","Mary","Mom","Jordyn","Presley","Daddy","Mommy","Grandma","Grandpa","Nana","Gepa");
}
As an additional option to h2oooooo' answer , you can use something like:
var sorted:Array = {
"easy":["Beau","Dad","Jesus","Mary","Mom"],
"medium":["Jordyn","Presley","Jesus","Mary","Nana"],
"hard":["Beau","Dad","Jesus","Mary","Mom","Jordyn","Presley",
"Daddy","Mommy","Grandma","Grandpa","Nana","Gepa"]
}[choice];
trace(sorted.constructor); // [class Array]

Randomly selecting an object property

I guess a step back is in order. My original question is at the bottom of this post for reference.
I am writing a word guessing game and wanted a way to:
1. Given a word length of 2 - 10 characters, randomly generate a valid english word to guess
2.given a 2 - 10 character guess, ensure that it is a valid english word.
I created a vector of 9 objects, one for each word length and dynamically created 172000
property/ value pairs using the words from a word list to name the properties and setting their value to true. The inner loop is:
for (i = 0; i < _WordCount[wordLength] - 2; i)
{
_WordsList[wordLength]["" + _WordsVector[wordLength][i++]] = true;
}
To validate a word , the following lookup returns true if valid:
function Validate(key:String):Boolean
{
return _WordsList[key.length - 2][key]
}
I transferred them from a vector to objects to take advantage of the hash take lookup of the properties. Haven't looked at how much memory this all takes but it's been a useful learning exercise.
I just wasn't sure how best to randomly choose a property from one of the objects. I was thinking of validating whatever method I chose by generating 1000 000 words and analyzing the statistics of the distribution.
So I suppose my question should really first be am I better off using some other approach such as keeping the lists in vectors and doing a search each time ?
Original question
Newbie first question:
I read a thread that said that traversal order in a for.. in is determined by a hash table and appears random.
I'm looking for a good way to randomly select a property in an object. Would the first element in a for .. in traversing the properties, or perhaps the random nth element in the iteration be truly random. I'd like to ensure that there is approximately an equal probability of accessing a given property. The Objects have between approximately 100 and 20000 properties. Other approaches ?
thanks.
Looking at the scenario you described in your edited question, I'd suggest using a Vector.<String> and your map object.
You can store all your keys in the vector and map them in the object, then you can select a random numeric key in the vector and use the result as a key in the map object.
To make it clear, take a look at this simple example:
var keys:Vector.<String> = new Vector.<String>();
var map:Object = { };
function add(key:String, value:*):void
{
keys.push(key);
map[key] = value;
}
function getRandom():*
{
var randomKey = keys[int(Math.random() * keys.length)];
return map[randomKey];
}
And you can use it like this:
add("a", "x");
add("b", "y");
add("c", "z");
var radomValue:* = getRandom();
Using Object instead of String
Instead of storing the strings you can store objects that have the string inside of them,
something like:
public class Word
{
public var value:String;
public var length:int;
public function Word(value:String)
{
this.value = value;
this.length = value.length;
}
}
Use this object as value instead of the string, but you need to change your map object to be a Dictionary:
var map:Dictionary = new Dictionary();
function add(key:Word, value:*):void
{
keys.push(key);
map[key] = value;
}
This way you won't duplicate every word (but will have a little class overhead).

Splice then re-index array in ActionScript 3

I want to remove the first four indexes from the array using splice(), then rebuild the array starting at index 0. How do I do this?
Array.index[0] = 'one';
Array.index[1] = 'two';
Array.index[2] = 'three';
Array.index[3] = 'four';
Array.index[4] = 'five';
Array.index[5] = 'six';
Array.index[6] = 'seven';
Array.index[7] = 'eight';
Array.splice(0, 4);
Array.index[0] = 'five';
Array.index[1] = 'six';
Array.index[2] = 'seven';
Array.index[3] = 'eight';
I am accessing the array via a timer, on each iteration I want to remove the first four indexes of the array. I assumed splice() would remove the indexes then rebuild the array starting at 0 index. it doesn't, so instead what I have done is created a 'deleteIndex' variable, on each iteration a +4 is added to deleteIndex.
var deleteIndex:int = 4;
function updateTimer(event:TimerEvent):void
{
Array.splice(0,deleteIndex);
deleteIndex = deleteIndex + 4;
}
What type of object is "Array" in the code you have shown? The Flash Array object does not have a property named "index". The Array class is dynamic, which means that it let's you add random properties to it at run time (which seems to be what you are doing).
In any case, if you are using the standard Flash Array class, it's splice() method updates the array indexes automatically. Here is a code example that proves it:
var a:Array = [1,2,3,4,5];
trace("third element: ", a[2]); // output: 3
a.splice(2,1); // delete 3rd element
trace(a); // output: 1,2,4,5
trace(a.length); // ouput: 4
trace("third element: ", a[2]); // output: 4
If I am understanding what you want correctly, you need to use the unshift method of Array.
example :
var someArray:Array = new Array(0,1,2,3,4,5,6,7,8);
someArray.splice(0,4);
somearray.unshift(5,6,7,8);
Also, you are using the Array Class improperly, you need to create an instance of an array to work with first.
The question is confusing because you used Array class name instead of an instance of an array. But as the commenter on this post said, if you splice elements, it automatically re-indexes.
im not sure what you want to do, but Array=Array.splice(0,4) should fix somethin..

Add a new property to an object stored with scriptDB

I created a database (scriptDB) and have stored information (more than 40 properties of about 600 people-students-).
var obj = {
alumne_id: email,
alumne_ordre: nomsencer_ordre,
alumne_timestamp: {created: new Date (). getTime (), changed:'', editor: 'unedited'},
alumne_nom: {nom: nomREAD, cognom1: cognom1READ, cognom2: cognom2READ, cognoms: cognomsREAD, nomsencer: nomsencerREAD},
(...)
}
 
I stored this object in my database:
var stored = db.save(ob);
I defined thus, properties of objects and have my stored. This works perfectly and update data, for example, change "alumne_nom.nom" to a new value: 'Joana'
Now I want to add new properties to all or some of these objects (students).
The problem is that I can add a property as
newproperty1
with:
stored.newproperty1 = '50 '
But I can not do:
stored.newproperty1.sub1
stored.newproperty1.sub2
stored.newproperty1.sub3
Does anyone know how I can add these subproperties?
To add "subproperties" you need to first define the property. You can do it by setting an empty object to it, e.g.
stored.newproperty1 = {};
//then all subs should work
stored.newproperty1.sub1 = 10;
You could already pass the subs definitions on the braces as you do with the object as well:
stored.newproperty1 = {sub1:10, sub2:20, etc:'value'};

AS3 How to make a kind of array that index things based on a object? but not being strict like dictionary

How to make a kind of array that index things based on a object? but not being strict like dictionary.
What I mean:
var a:Object = {a:3};
var b:Object = {a:3};
var dict:Dictionary = new Dictionary();
dict[a] = 'value for a';
// now I want to get the value for the last assignment
var value = dict[b];
// value doesn't exits :s
How to make something like that. TO not be to heavy as a lot of data will be flowing there.
I have an idea to use the toString() method but I would have to make custom classes.. I would like something fast..
Why not make a special class that encapsulates an array, put methods in there to add and remove elements from the array, and then you could make a special method (maybe getValueByObject(), whatever makes sense). Then you could do:
var mySpecialArrayClass:MySpecialArrayClass = MySpecialArrayClass();
var a:Object = {a:3};
var b:Object = {a:3};
mySpecialArrayClass.addElement(a,'value for a');
var value = mySpecialArrayClass.getValueByObject(a);
I could probably cook up a simple example of such a class if you don't follow.
Update:
Would something like this help?
http://snipplr.com/view/6494/action-script-to-string-serialization-and-deserialization/
Update:
Could you use the === functionality? if you say
if ( object === object )
it compares the underlying memory address to see if two objects are the same reference...