0
votes

I've got a properties file with two distinct key-value pair patters like the following;

  1. name.name.name.key = value
  2. name.name.fullname.key = value

Accessing them one by one is fine if the key is known. What I need now, though, is to access ALL values from say name.name.name without knowing the keys (integers to be exact). The values then have to be added to an ArrayList to be displayed in a List.

The keys are completely random, so I don't know the range beforehand (I can't and don't want to hardcode the range of range of keys for each single properties file), so simply looping through a previously defined range isn't what I need here. Also, the keys are not successive.

How can I do this with AS3?

Just for clarification as this seems to be a little confusing; I do not want to parse the properties file. I rather want to solve this using the methods AS and Flex already provide. The ResourceBundle class already has a method that outputs the content of the specified bundle. However, it contains all values. What I need is just a subset of values that match a given key pattern.

Edit: To make this easier, I can drop the key pattern matching and create a new resource file with a distinct pattern. Now I only need to get all key-value pairs (I need both the integers from the keys and the value) from that resource.

2
Hard to tell exactly what you need here. Are you trying to make a string parsing method that will result in a Dictionary where each key has a list of values? Or does each key have one value? - Ocelot20
I would like to avoid parsing the whole properties file as it is quite extensive and parsing it in real time would take too much time. I figure the ResourceManager is the way to go to solve this problem as it is made to get key-value pairs from given properties files. Each key does in fact have only one value. Getting a Dict from the properties file where the keys match a certain pattern would indeed be ideal. That's pretty much exactly what I'm after, but unfortunately the ResourceManager doesn't seem to provide such a method, or does it? - AlBirdie

2 Answers

2
votes

Here is a simple example with string parsing:

import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.Event;

var l:URLLoader = new URLLoader(new URLRequest("inp.txt"));
l.addEventListener(Event.COMPLETE, onComplete);


function onComplete(e:Event):void
{
    var data:String = e.target.data;
        //remove whitespace
    data = data.replace(' ','');
        //split data string by lines
    var pairs:Array = data.split("\r\n");
        //our name search pattern
    var pattern:RegExp = /name\.name\.name/;
        //array to store extracted values
    var values:Array = [];
    for each (var s:String in pairs) {
        if (pattern.test(s)) {
            //we've got a match, let's extract value
            var value:String = s.split("=")[1];
            values.push(value);
        }
    }
}
0
votes

The adapted String formatting method provided by package;

function onComplete(e:Event):void
{           
    var data:String = e.target.data;
    var pairs:Array = data.split("\r\n");

    var values:Array = [];
    for each (var s:String in pairs) {    
        var value:String = s.split("=")[1];
        values.push(value);

    }
    trace(values);

}

Not sure why, but the trace gives me the value of the first pair plus the key of the second pair. No idea why.

I actually though about adding the keys and values in a Dictionary, around the lines of this;

_dict[arr[0].substr(0, arr[0].length)] = arr[1];