Friday, November 28, 2008

@GetField accepts text-list as input

Just out of curiosity, I tried using a text-list as input for @GetField. It works.
When a text-list is the input, a text-list of the field values is the output.

fields := "subject" : "com_unique" : "relation";
@Implode( @GetField( fields ) ; "|" )

The above code gives me a pipe-separated string of the values in the subject, com_unique and relation-field.

Not the most useful tip I've blogged about, but probably nice to know.. :)


Ed Lee said...

Top tip, saves having to do multiple @GetField's

Thanks, Ed

Kevin Pettitt said...

Tommy, this is a very interesting if not incredibly useful tip. I'm not sure why you would ever use @GetField anyway since you could just reference the fieldname(s) directly in a formula.

However, I was momentarily very excited because I thought *maybe* this same field list technique works for @GetDocField, which pulls values from another document. If you're making several calls to the same document with @GetDocField, maybe you could combine them into one.

Alas, the answer appears to be NO.

Oh well, might not have made any difference in performance anyway.

Tommy Valand said...

@Kevin: I sometimes use the column formula in scripts, to ascertain the name of an item in a pipe-separated string.

The first line in the column formula may be:
fields := "firstName" : "lastName";
@Implode( @GetField( fields ) ; "|" )

Through some string-manipulation, I get the index of the fields, put them in a List of integers (ListTag - name specified in fields).

Add a function, for instance getFieldFromSeparatedString( ByVal separatedString As String, ByVal fieldName As String )

firstName = getFieldFromSeparatedString( someColumnValue, "firstName" )

The benefit of this method is that the index of the field in the separated string becomes non-important, and I feel it makes the code more readable.

If you're only going to use a value once, pick it from the separated string. firstName in the above example should be enough documentation of the code. No need to create a variable or a comment to document what the value is.

If getFieldFromSeparatedString( someColumnValue, "firstName" ) = "Tommy" Then Exit Sub

'Item 1 is firstName
If Strtoken( someColumnValue, "|", 1 ) = "Tommy" Then Exit Sub

Dim firstName As String
firstName = Strtoken( someColumnValue, "|", 1 )
If firstName = "Tommy" Then Exit Sub

In the above pseudocode, I don't include the List of indexes. In most cases, this would probably be an input parameter to getFieldFromSeparatedString. Or, if you write OO code, the List would be a private member of the Class. Pick the indexes at the instantiation of the Class.