• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Converting Array and String

Guest
May 14, 2012 May 14, 2012

Copy link to clipboard

Copied

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.

TOPICS
ActionScript

Views

4.3K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
May 15, 2012 May 15, 2012

Copy link to clipboard

Copied

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;

                    }

          }

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
May 15, 2012 May 15, 2012

Copy link to clipboard

Copied

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...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
May 16, 2012 May 16, 2012

Copy link to clipboard

Copied

Hm...

The String, which is going to server, looks like that:

FENCE_004|1|2|3&FENCE_004|0|0|0|4,5,6&ZZZ|8

So far, it is all  quite clear.

The Array, created after loading that String looks like this:

FENCE_004,1,2,3   amp;FENCE_004,0,0,0,4,5,6   amp;ZZZ,8

Where spaces are new line.

I don't understand, why code is not removing &  and turning it to amp; in the beginning of next item...

________________________________________

Found why:

Loaded String looks like

FENCE_004|1|2|3&FENCE_004|0|0|0|4,5,6&ZZZ|8

________________________________________

Why server translated that symbol to another one? Are there any, which wouldn't?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
May 22, 2012 May 22, 2012

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
May 22, 2012 May 22, 2012

Copy link to clipboard

Copied

Of course, I

could replace all the instances of & with & using String.replace()

but, it is better not to save & to server at all (limited space), so I've used tilda, not &, when converting to String. It was saved and loaded as is, as tilda... But I think it's not a kind of special symbol.

Unfortunately, charCodeAt returns numbers, so there would be no way to understand, where was Number or charCode


As far as I've understood, server is 'talking' on HTML, so it has symbols, which would  be 'translated' before saving them, because it also uses the same when 'talking'. The example is & translated to &

What special symbols (not used normally by user) can be used in HTML not being 'translated'?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
May 28, 2012 May 28, 2012

Copy link to clipboard

Copied

With

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

you can recover the character from the ASCII code

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
May 27, 2012 May 27, 2012

Copy link to clipboard

Copied

Anyone?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 28, 2012 May 28, 2012

Copy link to clipboard

Copied

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/

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
May 28, 2012 May 28, 2012

Copy link to clipboard

Copied

to kennethkawamoto2

As I've mentioned before, I have limited space, and JSON returns String full of odd symbols, which I have to save to server.

to esdebon

As I've mentioned before, charCodeAt() rerturns numbers. We have

FENCE_004|1|2|3&FENCE_004|0|0|0|4,5,6&ZZZ|8

If I have to make symbol & to Unicode, the resulting String will look like this:

FENCE_004|1|2|338FENCE_004|0|0|0|4,5,638ZZZ|8

There is no way to understand, where to start using fromCharCode(), because 3& turned to be 338

I'm just asking for advice: what symbols are not 'translated' by HTML (because & is)?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
May 29, 2012 May 29, 2012

Copy link to clipboard

Copied

LATEST

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.


Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
May 22, 2012 May 22, 2012

Copy link to clipboard

Copied

You can convert each character to the ascii value using:

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

And get back:

String.fromCharCode(temp);

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
May 22, 2012 May 22, 2012

Copy link to clipboard

Copied

I tried to use tilda, it worked, wasn't 'translated', but I think tilda is not a special symbol and may be used by user, which would make the array break in wrong way...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines