Multidimensional arrays in YAML

Discussion in 'Plugin Development' started by kumpelblase2, Feb 4, 2012.

Thread Status:
Not open for further replies.
  1. Hey guys,

    is there a way to get/set multidimensional arrays in YAML?
    I'm thinking of this structure:
    Code:
    ...
    edges:
        -   - 12
            - 43
            - 74
        -   - 34
            - 203
            - 1
    ...
    Or is this not valid?
     
  2. Offline

    hatstand

    This should answer your question, and though it deals with Ruby, not Java, the example yaml should still work fine.

    (In short, that should be valid)
     
  3. Ok, thanks hatstand.

    so how do you get them via the FileConfiguration?
    I only saw getMapList() which would return sort of a multidemsional-thingy (which doesn't fit to my content), so how would I get what i want?

    EDIT:
    Might it be getList(path, List<List<double>>)?
    How about get() and then casting it to List<List<Double>>?
     
  4. Offline

    hatstand

    I'd say you're on the right track using get() and casting to a list of lists - Best way to find out is to test
     
  5. New question:
    Code:
    ...
    mobs:
        - name: Creeper
          probability: 40
        - name: Zombie
          probability: 60
    ...
    how would I now get the name of the first or second entry in the list e.g.?
     
  6. Well why don't you then. Your posting for help - I'm offering a solution. You don't have to be so harsh.
     
  7. Offline

    nisovin

    Well, it doesn't really answer his question, now does it? It's like someone asking, "Hey, how do I get to New York?" And you say, "Well, you could buy this new car I just made."
     
    kumpelblase2 likes this.
  8. Offline

    hatstand

    For something like that, you would be better off doing this:
    Code:
    ...
    mobs:
        - Creeper:
          probability: 40
        - Zombie:
          probability: 60
    ...
    Then you can use the names themselves as the keys
     
  9. The problem with that solution might be that when I have the mob more than one time, meaning that the key exists more than once, it would fail wouldn't it?
    so like this:
    Code:
    ...
    mobs:
        - Creeper:
          probability: 40
        - Zombie:
          probability: 40
        - Creeper:
          probability: 20
    ...
    I don't think that this would still work...
     
  10. Offline

    hatstand

    Then you need to use something else, like a number, as the key.
     
  11. No, not at all. It's more like he's asking how do I get to New York and I'm offering a different map.
     
  12. Bump.

    So it's not doable with the current configuration api?
     
  13. Not easily.
     
  14. Offline

    fullwall

    kumpelblase2 - this is why Citizens substitutes numbers in as a placeholder node for quests.
     
  15. Offline

    JayEffKay

    Actually I've done something like this. According to the javadocs YamlConfiguration.getMapList it will read a yaml-list, return a List<Map<String, Object>>, and try to parse each node in that list accordingly. Any nodes in the list that are yaml-lists themselves are parsed as map-list as well, so everything should go pretty automagically. And for me it did, at least up to two levels. I'll whip up an example:

    Code:
    //this will give out a warning of unchecked ArrayList<?> type, but there's noway any other list than <Map<String, Object>> comes out of the yaml function.
     
    YamlConfiguration myYaml = new YamlConfiguration;
    myYaml.load("myfile.yml"); //don't do this, use a try/catch around it
    List <Map<String, Object>> list = new ArrayList<Map<String, Object>>;
    List <Map<String, Object>> listInList = new ArrayList<Map<String, Object>>;
    list = myYaml.getMapList("mainlist");
    for (Map<String, Object> m: list) {
        //do stuff here with each list-entry, one of those entrys will be listinlist
        if (m.contains("listinlist") && m.get("listinlist") instanceof ArrayList<?>) {
            listInList = (ArrayList<Map<String, Object>>) m.get("listinlist")
            for (Map<String, Object> lm: listInList) {
                //do stuff here with each listinlist-entry
            }
        }
    }
    
     
    devilquak likes this.
Thread Status:
Not open for further replies.

Share This Page