Skip to main content
May 15, 2012
Question

Converting Array and String

  • May 15, 2012
  • 1 reply
  • 4831 views

Hello, everyone.

I'm given an Array, it is not in format {abc:1, qwe:'ghj'}. The array looks like this:

new Array(["SMTH", 1, 2, 3], ["ANTH", 0, 0, 0, [4, 5, 6]], ["ZZZ", 8])

As you can see, it has only square brackets, and even in other square brackets.

The task is:

1) Convert this Array to String, because server only saves strings. Save it.

2) Load saved String and convert it to Array.

So, a usual task for the game.

Please, tell me, in what Language they may be converted, what should I do.

1) I've tried to convert (1) Array to String, so that square brackets remain, even in another [], but AS3 removes them. Well, I wrote my own function. Now the result (1) looks exactly like you saw above in bold letters.

2) But another problem exists: how to convert that string to Array back...

Array.match() says there is no array, seems like it doesn't notice square brackets at all.

I've tried split, IndexOf and others, but got stuck in those [[]]

Simple myString as Array also doesn't work.

I suppose, I'm doing it all wrong. So i've tried converting Array to JSON. It turned out to be too much odd symbols, it was saved, but i have an limitation of symbols count, so JSON is not the answer.

This topic has been closed for replies.

1 reply

_spoboyle
Inspiring
May 15, 2012

there is probably a cleaner way to do thisn using recursion but this will do what you ask for i think

you could find solutions buy doign a google search for serialize

package 

{

          import flash.display.Sprite;

          public class Main5 extends Sprite

          {

                    public function Main5()

                    {

                              var array:Array = new Array(["SMTH", 1, 2, 3], ["ANTH", 0, 0, 0, [4, 5, 6]], ["ZZZ", 8]);

                              trace("array: " + array);

                              var string:String = convertToString(array);

                              trace("string: " + string);

                              var array2:Array = convertToArray(string);

                              trace("array2: " + array2);

                    }

                    private function convertToString(array:Array):String

                    {

                              var output:String = "";

                              for (var i:int = 0; i < array.length; i++)

                              {

                                        var level1Items:Array = array as Array;

                                        for (var j:int = 0; j < level1Items.length; j++)

                                        {

                                                  var level1Item:* = level1Items;

                                                  if (level1Item is Array)

                                                  {

                                                            for (var k:int = 0; k < level1Item.length; k++)

                                                            {

                                                                      output += level1Item;

                                                                      if (k < level1Item.length -1)

                                                                      {

                                                                                output += ","

                                                                      }

                                                            }

                                                  }

                                                  else

                                                  {

                                                            output += level1Item;

                                                            if (j < level1Items.length - 1)

                                                            {

                                                                      output += "|";

                                                            }

                                                  }

                                        }

                                        if (i < array.length - 1)

                                        {

                                                  output += "&";

                                        }

                              }

                              return output;

                    }

                    private function convertToArray(string:String):Array

                    {

                              var output:Array = new Array();

                              var level1Items:Array = string.split("&");

                              for (var i:int = 0; i < level1Items.length; i++)

                              {

                                        output = new Array();

                                        var level1Item:String = level1Items;

                                        var level2Items:Array = level1Item.split("|");

                                        for (var j:int = 0; j < level2Items.length; j++)

                                        {

                                                  var level2Item:String = level2Items;

                                                  if (level2Item.indexOf(",") > -1)

                                                  {

                                                            output = new Array();

                                                            var level3Items:Array = level2Item.split(",");

                                                            for (var k:int = 0; k < level3Items.length; k++)

                                                            {

                                                                      output = level3Items;

                                                            }

                                                  }

                                                  else

                                                  {

                                                            output = level2Item;

                                                  }

                                        }

                              }

                              return output;

                    }

          }

}

May 15, 2012

Wow, seems like your function convertToString is almost copy of mine I've also tried to separate array Items not only by square brackets.

Now I'm gonna try using convertToArray, thanks beforehand!!! I'll write about results.

PS.Only I have this forum viewing strangely? I remember that two weeks ago it looked more pretty...