LAE

Welcome to the LAE community!  Please feel free to start a discussion in the discussion tab or join in a conversation.

Discussions

Members

Resources

Events

 View Only
  • 1.  list of fields(column headers) into a variable....

    Employee
    Posted 11-18-2014 05:01

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: dhrobertson

    hi all,

    I'm sure there is a very simple way of doing this but I'm currently having a brain freeze...

    I want to work out if a particular field(column name) exists in the input and if so then to do something, else if not do something else.

    in other scripting languages there are functions like If exists 'field' then do something.
    however in BrainScript I can find an equivalent. I have hit on a function called inputFields which, from my interpretation of the help files, suggests that if I say something like:

    temp = inputFields("in1")

    then I should get a list in temp of the column headers in input 1. maybe I don't understand the help correctly....

    If I do this and then try and emit temp, it fails with error: Unable to Print value of type: const-array

    what am I doing wrong? and if what I want to achieve is not possible with inputFields, is there some other way of listing the column headers into a variable, after which I can then do a strFindI for that relevant column name and if exists then do an IF statement etc.etc

    any help appreciated!

    thanks

    doug


  • 2.  RE: list of fields(column headers) into a variable....

    Employee
    Posted 11-18-2014 06:59

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: stonysmith

    temp = inputFields("in1") works properly, but it produces a LIST datatype, instead of a string.
    While in Brainscript, you can use any of the LIST operators against "temp" such as
    i=find(temp,"color")  #returns the position in the list of the field named "color"
    At this time, LAE can not EMIT a LIST datatype, you'd need to convert it to a string. (there is an open enhancement request to be able to emit LIST datatypes)

    The simplest way to do this is:

    emit temp=inputFields("in1").str()
    which will return a column like this:
    "{color;id;type;rand;junk}"

    It's rather awkward, but for a comma-separated list, what I prefer to use is this:
    emit temp=inputFields("in1").str().replace("{","").replace("}","").replace(";",",")
    edit: fixed the last replace() above.