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.  Convert to string or integer

    Employee
    Posted 08-16-2016 05:40

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

    Originally posted by: s_oosting

    Hi,

    I am new user. And I'm trying to convert data to string or integer (depending on the input data).

    I tried the following code. But even if al items are numbers it still converts to string, not integer.

    if isNumber(nr)==true then transactionID = int(nr) else transactionID = str(nr)

    Thanks!


  • 2.  RE: Convert to string or integer

    Employee
    Posted 08-16-2016 07:40

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

    Originally posted by: awilliams1024

    Hi,

    For a particular field, all its records (cells) must have the same data type - unlike with, say, Microsoft Excel.

    In the case of your code, the data type of the transactionID field is being determined when the node logic is compiled (i.e. before the first record is processed), and as the final assignment for the transactionID is to a string variable, the output field is given a string data type.

    The simplest way to set the data type for a field is to use the Data Analyzer node (which can be found in the publishing and patterns category of the library) to scan the data and automatically convert the field to the most appropriate data type.

    However, this node will attempt to convert all fields in the data set. If you do not want to modify all the fields in the data set then it would be necessary to split out the field(s) to be processed by the Data Analyzer node from the other fields and join them together when the data type analysis and conversion has been completed. The attached data flow shows how this can be achieved. When the original data set has been split an additional record ID field is inserted so that the data sets can be reliably merged together after the conversion has been performed.

    In the example the first test data set has all numeric values in the 'nr' field. In the second (lower) test data set there is a non-numeric value in the third record. To show that all records are being converted by the Data Analyzer node, the 'rand' field has all numeric values but uses a string data type field.


    Regards,
    Adrian
    Attachments:
    Convert_to_string_or_integer.brg


  • 3.  RE: Convert to string or integer

    Employee
    Posted 08-16-2016 11:24

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

    Originally posted by: rbeliz000

    Hi,

    I am looking for a way to modify the data fields "tier_up" & "tier_down" (see attached file) so that the characters to the right of the decimal are removed. To have "10.000" replaced with "10". The original field is in a string format.

    I have tried the following and neither worked:
    emit tier_down.double() as "modified_down" and emit tier_down.int() as "modified_down"

    neither did the job.
    Please reference the attached file for more information.

    Any assistance would be greatly appreciated.

    thanks.

    tier_up tier_down modified_up modified_down
    10.000 25.000 10 25


  • 4.  RE: Convert to string or integer

    Employee
    Posted 08-16-2016 20:51

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

    Originally posted by: stonysmith

    emit tier_down.int() as "modified_down"
    Should have done the job. What tool are you using to view the data? The BRDViewer, or Excel, or some other output method.

    In the BRDViewer you should be able to confirm that the output above is "integer" datata type.


  • 5.  RE: Convert to string or integer

    Employee
    Posted 08-17-2016 00:50

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

    Originally posted by: awilliams1024

    Hi,

    The code for converting the string field value to a double worked for me. However there is an issue in converting string values directly to integer causing the node to error.

    You could use the following two-step approach to convert the string values to a double:

    emit tier_down.double().int() as "modified_down"

    Alternatively, you could get the integer part of the tier_down string value by trimming the string at the decimal point and then convert that to an integer:

    idx = tier_down.strFind(".")
    if idx > -1 then stem = tier_down.left(idx) else stem = tier_down
    emit stem.int() as "modified_down"

    Regards,
    Adrian


  • 6.  RE: Convert to string or integer

    Employee
    Posted 08-17-2016 05:47

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

    Originally posted by: rbeliz000

    I uploaded a sample graph which replicates the error for your reference.

    Below is the error I get using the interger option.

    WARN: could not convert to integer: '25.000'
    Line: 2; BrainScript: emit tier_down.int() as "modded_tier_down"
    Operator: 'int'
    Error Code: brain.node.ToInteger_ExprOp.cpp.123

    ERROR: Node execution terminated while processing data by error:
    could not convert to integer: '25.000'
    Error Code: lae.node.executionTerminated
    Attachments:
    interger_graph_filter.brg
    interger_graph_filter.brp


  • 7.  RE: Convert to string or integer

    Employee
    Posted 08-17-2016 10:16

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

    Originally posted by: awilliams1024

    Thanks for the example which errors as you describe.

    BTW, you can also use the Data Analyzer node to also perform the conversion of these fields to integer data type.

    Regards,
    Adrian


  • 8.  RE: Convert to string or integer

    Employee
    Posted 08-17-2016 10:56

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

    Originally posted by: rbeliz000

    Hi Adrian,
    Thank you, the analysis node worked.

    Take a look at the attached updated graph and let me know if you have any pointers on further streamlining the code.

    thanks a bunch.
    Attachments:
    interger_graph_filter.brg
    interger_graph_filter.brp


  • 9.  RE: Convert to string or integer

    Employee
    Posted 08-17-2016 10:58

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

    Originally posted by: prasmussen

    Here's another method. This could be simplified; however, without being familiar with your data quality I recommend putting in some validations.

    value = "25.000"
    if value.isNull() or value.trim() == "" then { newValue=null.int() }
    else if value.strFindI(".") > -1 then { newValue = value.substr(0,value.strFind(".")).int()}
    else abort("Could Not convert " + value + " to integer")

    emit newValue, value