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.  Substring logic

    Employee
    Posted 07-24-2014 09:20

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

    Originally posted by: sree

    Hi Team,

    My data is like below
    Employee Name:
    ----------------
    Mark Yun
    Reaney Dorsey Kunkler
    Maryanne Himmelsbach

    My requirement is to load first,last name from name,Separate data by a ' ' (space) into two fields, first name and last name.

    First Name:
    -----------
    Mark
    Reaney
    Maryanne

    Last Name:
    ----------
    Yun
    Dorsey Kunkler
    Himmelsbach.
    Please let me know what function\logic need to use to get first , last name seperately.

    Thanks,
    Sree.


  • 2.  RE: Substring logic

    Employee
    Posted 07-24-2014 11:13

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

    Originally posted by: rnukovic

    Use a Filter node and look at the following BRAINscript operators:
    strFind, left, substr


  • 3.  RE: Substring logic

    Employee
    Posted 07-24-2014 23:06

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

    Originally posted by: sree

    Originally posted by: rnukovic
    					

    Use a Filter node and look at the following BRAINscript operators:
    strFind, left, substr
    Thank you ,Can you please give exact syntax for this, i tried using strFind, left, substr, but its not worked.
    Once again thank you for your help.


  • 4.  RE: Substring logic

    Employee
    Posted 07-25-2014 09:21

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

    Originally posted by: rnukovic

    If you put the BRAINscript here, I can help you figure out why it's not working.


  • 5.  RE: Substring logic

    Employee
    Posted 08-04-2014 21:29

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

    Originally posted by: sree

    Hi,

    by taking sample input data
    Name
    Mark Yun
    Reaney Dorsey Kunkler
    Maryanne Himmelsbach

    I tried following logic in filter node.

    firstname = substr('Name','',0)
    lastname = find('Name','',1)
    name1 = strFind('Name','',1)
    emit
    firstname as "First Name",
    lastname as "Last Name",
    name1 as "Last Name1"

    when excuted the graph getting the following error
    "ERROR initializing expert output:
    unrecognized token: ''''
    2014-08-05 09:49:53.000; WARN: unrecognized token: ''''
    id: 0 chain: 0 group: 0
    cppDetail: context: Expr::parseExpr() ..\expert\Expr.cpp@82

    2014-08-05 09:49:53.000; WARN: ...in expression starting on line 1
    id: 0 chain: 0 group: 0
    cppDetail: context: Expr::parseExpr() ..\expert\Expr.cpp@89

    2014-08-05 09:49:53.000; WARN: ...in expression starting on line 1".

    Required output like this :-
    -------------------------

    first name :-
    -------------
    Mark
    Reaney
    Maryanne

    Lst name :-
    -----------

    Yun
    Dorsey Kunkler
    Himmelsbach

    Require populate before ""(delimeter) name as first name, After ""(delimeter) name as last name.
    if any name had more than 2 words like,first name,middle name,last name,no need to consider middle name.
    Please help me what need to write function\logic to get firstname and last name.


  • 6.  RE: Substring logic

    Employee
    Posted 08-05-2014 00:11

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

    Originally posted by: Tim Meagher

    Hi,

    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.


  • 7.  RE: Substring logic

    Employee
    Posted 08-05-2014 17:40

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

    Originally posted by: stonysmith

    Tim's method above works fine.. here is another possible solution:

    s=split('Name'," ") # use single quotes for the column name, double quotes for the string to split with
    #the split() operator breaks a string up into an array
    firstname=s[0] # initial position in the array
    lastname=s[len(s)-1] # last position in the array