Skip navigation
Currently Being Moderated

Converting Array and String

May 14, 2012 9:59 PM

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.

 
Replies
  • Currently Being Moderated
    May 15, 2012 1:34 AM   in reply to XELAD111

    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[i] as Array;

     

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

                                            {

                                                      var level1Item:* = level1Items[j];

     

                                                      if (level1Item is Array)

                                                      {

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

                                                                {

                                                                          output += level1Item[k];

                                                                          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[i] = new Array();

     

                                            var level1Item:String = level1Items[i];

     

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

     

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

                                            {

                                                      var level2Item:String = level2Items[j];

     

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

                                                      {

                                                                output[i][j] = new Array();

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

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

                                                                {

                                                                          output[i][j][k] = level3Items[k];

                                                                }

                                                      }

                                                      else

                                                      {

                                                                output[i][j] = level2Item;

                                                      }

                                            }

                                  }

     

                                  return output;

                        }

     

              }

     

     

    }

     
    |
    Mark as:
  • Currently Being Moderated
    May 22, 2012 1:42 AM   in reply to XELAD111

    before converting the string to an array you could replace all the instances of &amp; with & using String.replace()

     
    |
    Mark as:
  • Currently Being Moderated
    May 22, 2012 10:17 AM   in reply to XELAD111

    You can convert each character to the ascii value using:

     

    arrayAscii[i] = c.charCodeAt(i);  //where c is a String, i the character place in the string

     

     

    And get back:

     

    String.fromCharCode(temp);

     
    |
    Mark as:
  • Currently Being Moderated
    May 28, 2012 4:29 AM   in reply to XELAD111

    Why not just use JSON?

     

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

    var jsonArray:String = JSON.stringify(array);

    var revivedArray:Array = JSON.parse(jsonArray) as Array;

    trace(revivedArray[1][4][2]); // traces "6"

     

    --

    Kenneth Kawamoto

    http://www.materiaprima.co.uk/

     
    |
    Mark as:
  • Currently Being Moderated
    May 28, 2012 7:42 AM   in reply to XELAD111

    With

     

    String.fromCharCode(temp)    //where temp is a number

     

    you can recover the character from the ASCII code

     
    |
    Mark as:
  • Currently Being Moderated
    May 29, 2012 8:13 AM   in reply to XELAD111

    Almost all characters aren´t A-Z, a-z, 0-9,-_, be modified by HTML, mentioned as a solution convert to numbers, so is that I have solved your same problem on several projects, obviously using a character that mark separation between characters.

     


     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points