Results 1 to 2 of 2

Thread: Records in ASH

  1. #1
    Developer Veracity's Avatar
    Join Date
    Mar 2006
    Location
    The Unseelie Court
    Posts
    13,659

    Default Records in ASH

    Starting with SVN revision 1311 of KoLmafia, ASH now supports a new kind of structured data: the record. Here is a little example of how you declare a record and variables of the new type you've created by doing so.

    Code:
    record my_type {
        int ifield;
        string sfield;
        record {
            int first;
            int second;
        } rfield;
        int [int, int] mfield;
    };
    
    my_type rvar;
    my_type [int] mrvar;
    What I've done with the above is declare a new data type which I've named "my_type". Having declared the new type, I can use it (almost) anywhere that I can use a built-in type name. I declared a variable, "rvar", of that type, and I defined a map, "mrvar", which maps keys of type integer to values of type my_type.

    The new type, "my_type" is a "composite" type. It contains four fields. "ifield" is an integer. "sfield" is a string. "rfield" is another composite field: an anonymous record containing two integers named "first" and "second". Finally, "mfield" is a map from [int, int] to int.

    As you can see, a record can combine data of all the types ASH supports: primitive, aggregate, and composite.

    Having defined the new data type and several variables using it, here are some examples of how to access the fields.

    Code:
    rvar.ifield = 10;
    rvar.sfield = "secret";
    rvar.rfield.first = 1000;
    rvar.sfield.second = 2000;
    rvar.mfield[ 2, 3 ] = 12;
    
    mrvar[ 1 ] = rvar;
    
    foreach key in mrvar
        foreach key1, key2 in mrvar[key].mfield
            print( "val = " + mrvar[key].mfield[key1,key2] );
    As you can see, if you have a variable that is a record, you access the fields of the record by following the variable name with ".<field name>". The resulting value will be of whatever type you declared in the definition of the record. If the value is a map, you can give a list of keys within [], just like any other map. If the value is another record, you can access the fields of the nested record by using another ".<field name>".

    If you are familiar with Pascal "records" or C/C++ "structs", this should all be comfortably familiar.

    Finally, if you create a map whose values is a record, the file_to_map and map_to_file built-in ASH functions will Do The Right Thing; they will efficiently and reliably save and restore your data.
    Ph'nglui mglw'nafh Cthulhu
    R'lyeh wgah-nagl fhtagn.

  2. #2
    Senior Member
    Join Date
    Apr 2019
    Posts
    282

    Default

    Little error I noted:
    Code:
    rvar.sfield.second = 2000;
    should be
    Code:
    rvar.rfield.second = 2000;
    (difference being sfield => rfield)

Similar Threads

  1. Records without a name and temp records
    By Fluxxdog in forum Scripting Discussion
    Replies: 0
    Last Post: 11-20-2013, 05:47 PM
  2. Feature Once Per Combat Proxy Records
    By Catch-22 in forum Bug Reports
    Replies: 10
    Last Post: 07-07-2012, 10:17 PM
  3. Feature More Proxy Records for Familiars
    By Catch-22 in forum Bug Reports
    Replies: 5
    Last Post: 07-05-2012, 08:41 AM
  4. Help with maps of records
    By StDoodle in forum Scripting Discussion
    Replies: 5
    Last Post: 12-05-2010, 04:35 AM
  5. Consumption records
    By efilnikufecin in forum Informational Scripts
    Replies: 0
    Last Post: 12-18-2006, 11:33 AM

Posting Permissions

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