Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.
Originally posted by: Tim MeagherHi,
First, the single quotes within BRAINscript are used to refer to a field - while you seem to be using them in some places to refer to string literals.
If you have something like:
x = '
F'
It will locate the value of the field
F on the current record being processed and assign it to the variable
x.
For future reference, this is described in the BRAINscript help under the section "Simple Expressions".
String literals are referenced in BRAINscript using the double quote.
If you have something like:
x = "
F"
It will assign the string
F to the variable
x.
For future reference, this is also described in the BRAINscript help under the section "Simple Expressions".
Now since single quotes are used to reference a field on the current input record, using empty single quotes will never work.
This is effectively trying to locate a field with no name.
Therefore, the following just can't work.
firstname = substr('Name','',0)Simply replacing this with double quotes ("") won't do what you want either, for a number of reasons.
First - I think you want to look for a space character, which would be " " rather than "".
Second - if you look at the help for the substr operator in the BRAINscript help, you will see that when 3 arguments are provided, the first argument "input" needs to be a string (in your case the value of the field "Name", which is correct). However, the second argument "offset" needs to be an integer or long value representing the character offset into the string where the sub-string is to begin.
The third argument "num" if provided needs to be the length of the sub-string.
Looking at your use of strFind, it can't work either:
name1 = strFind('Name','',1)The reference to a field with no name ('') is incorrect as described previously.
Further, if you look at the BRAINscript help for the strFind operator, you will see that it takes only two arguments - not three.
The first argument is the string within which the find is to be performed (in your case, this would be the value of the field "Name" which is correct).
The second argument is the string which is to be located within that first string.
The BRAINscript help shows several examples of how this can be used.
Since the strFind operator doesn't take a 3rd argument, and doesn't take any integer arguments, the third argument you have provided (1) is incorrect.
In order to use these operators correctly, its worthwhile looking at the BRAINscript help (found via "Help"->"BRAINscript Help" within BRE).
First, looking at strFind.
You'll see the following example for using strFind:
strFind("abcdefg", "ef") # value: 4
What this says is:
Take the input string literal "abcdefg" and locate the first occurrence of the substring "ef" within that string, and return the index where that substring begins.
Since strings are 0-indexed:
- The character "a" occurs at index 0
- The character "b" occurs at index 1
- The character "c" occurs at index 2
- The character "d" occurs at index 3
- The character "e" occurs at index 4
- etc
Looking at index 4, you will see that the substring "ef" starts at index 4, so the returned value from the strFind operator for this example will be 4.
In your case, you want to locate where there is a space (" ") in the string to split out the first and last names.
Therefore, you can use:
index = strFind('Name', " ")
This will then return the index where the first space character is found within the field "Name".
Once you have that index, you can then look at using the substr and left operators.
Looking at the documentation for the left operator, you'll see the following example:
left("abcdefg" 3) # value: "abc"
The left operator takes an input string, and an integer argument
num and returns the first
num characters from the input string.
In the above example, this is returning a string containing the first 3 characters from the string "abcdefg" which equals "abc".
Therefore, once you have used the strFind operator as mentioned above, you know the index of the first space within the Name field, and have assigned this to the variable
index.
You then want to extract all the characters in the field "Name" prior to that first space.
Therefore, in order to obtain the first name, you can use:
firstname = left('Name', index)
Now onto the last name.
This is where you need to use the substr operator.
Looking at the help for this operator, you'll see that it can either take 2 or 3 arguments.
In the 2 argument form:
substr(input, offset) it will provide a substring of the
input string starting at the specified
offset, and containing all of the remaining characters in the input string.
In the 3 argument form:
substr(input, offset, num) it will provide a substring of the
input string starting at the specified
offset, and containing only
num characters.
In your case, for the last name, you want all of the contents of the field "Name"
after the space character.
Therefore, you only need the 2 argument form.
I've already shown how you can locate the offset of the the space character in a previous code snippet for the strFind operator, and shown how you can assign this to the variable
index.
First the last name, you don't want to include the space character - you want to start the substring on the character after the space character.
This is then the index of the space character, plus 1.
Therefore, to obtain the last name, you can use:
lastname = substr('Name', index+1)
Then you'll have the first name and last name extracted and assigned to variables that you can output.
Regards,
Tim.