Results 1 to 3 of 3

Thread: Does ASH have a way of testing value inclusion in ordered collections?

  1. #1
    Junior Member five35's Avatar
    Join Date
    Jan 2015
    Posts
    7

    Question Does ASH have a way of testing value inclusion in ordered collections?

    I need to create a collection which I can iterate over in a specific order as well as test for inclusion. Because maps always sort by key and can't be tested for value inclusion (that I know of; I'm very new to ASH scripting), I haven't been able to implement this as a single data structure. Instead, I'm currently using two; one for iteration order and another for inclusion testing (see below). Is there a way around this?

    Code:
    string[3] MY_DATA_ORDERED;
    
    MY_DATA_ORDERED[0] = "First";
    MY_DATA_ORDERED[1] = "Second";
    MY_DATA_ORDERED[2] = "Third";
    
    boolean[string] MY_DATA;
    
    foreach index in MY_DATA_ORDERED MY_DATA[MY_DATA_ORDERED[index]] = true;
    
    // Now I can iterate in order:
    foreach index in MY_DATA_ORDERED print(MY_DATA_ORDERED[index]);
    
    // Or test for inclusion:
    if (MY_DATA contains "Second") print("All is well.");
    if (MY_DATA contains "Fourth") print("Something screwy is going on!");
    Last edited by five35; 11-26-2017 at 10:29 PM.

  2. #2
    Developer
    Join Date
    Aug 2009
    Posts
    2,709

    Default

    Sort of.

    Plural typed constants are technically ArrayLists under the hood, and iteration order is the same as definition order.

    e.g.

    Code:
    boolean [string] foo = $strings[a,b,c,a];
    
    foreach s in foo {
    	print(s);
    }
    
    print(foo contains "a");
    > call test.ash
    a
    b
    c
    a
    true
    That said, if you want to work with a dynamic array, building an inverted index (or something similar, e.g. what you're doing) is the most "obvious" way to solve your problem (assuming your map value is a primitive type).

  3. #3
    Junior Member five35's Avatar
    Join Date
    Jan 2015
    Posts
    7

    Default

    Interesting! I had no idea plural typed constants could be used that way. I'll dig into them and see if I can come up with something that's easier to read than what I have now.

    Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •