YailList - test if it is a flat list (or list of lists) and parse

I can do this with blocks, but would like to create a little helper extension to do this.

Need to:

  1. Import the list
  2. get the first item
  3. Test if first item is an "element" (e.g. string or integer)
  4. If it is, then return the list
  5. If it isn't, then parse the list, and return it

I can probably do the parsing code (JsonObjectEncode/JsonTextDecode), I have that already, I am struggling with items 1-3.

## PSEUDO-CODE

@SimpleFunction(description = "fixes multi-column lists from SELECT")
  public YailList ListFixer(YailList queryOutput) {
     String[] queryList;   
    // ??
    if (get.queryOutput(0).isString() || get.queryOutput(0).isInteger()) {
          return queryOutput;
    } else {
          queryList = JsonStringify(queryOutput);
    }
    return YailList.makeList(queryList);
  }
1 Like
    @SimpleFunction
    public Object ListFixer(YailList items, int pos) {
        Object[] objects = items.toArray();
        if (objects[0] instanceof IntNum) {
            // the first element is AppInventor number type
        }
    }
2 Likes

Thank you - what if objects[0] is a string ? (Also why the "int pos" is this used?)

You can simply ignore that.

You have to check it the same way:

    @SimpleFunction
    public Object ListFixer(YailList items, int pos) {
        Object[] objects = items.toArray();
        Object firstElement = objects[0];
        if (firstElement instanceof IntNum) {
            // first element is a number
        } else if (firstElement instanceof String) {
            // first element is a string
        } else {
            // the first element is not a Number or a string
        }
    }
1 Like

Great, I understand :slight_smile:

Given I can only return an Object, can I make this a YailList ?

} else {
// the first element is not a Number or a string
}
return ???YailList as Object???
}

You dont necessarily need to return it as Object, yes you can make that YailList.

@SimpleFunction public YailList ListFixer(YailList items) {...}
1 Like

I'll try it all out and report back :smiley:
Thanks for your help!

1 Like

Well, you could knock my down with a feather! Worked perfectly on first build. Needed an Object after all.
Thank you so much :slight_smile:

@SimpleFunction(description = "fixes multi-column lists from SELECT")
  public Object ListFixer(YailList queryOutput) {
    Object[] objects = queryOutput.toArray();
    Object firstElement = objects[0];
    if ((firstElement instanceof IntNum) || (firstElement instanceof String)) {
      return queryOutput;
    } else {
      return JsonParse(JsonStringify(objects),true);
    }
  }
2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.