Hitachi Vantara Pentaho Community Forums
Results 1 to 10 of 10

Thread: Modified Java Script to compare two fields

  1. #1

    Default Modified Java Script to compare two fields

    var country_score

    Hi all,

    I have a Java Script that should compare string values in two fields. Here is the code that returns 1 even when Country_ISO!=Country_ISO_1. Can someone please tell me what am I doing wrong?


    var country_score


    if (Country_ISO!=null)
    {
    if (Country_ISO=Country_ISO_1)
    {var country_score=1;}
    else {var country_score=0;}
    }


    else
    { if (Country=Country_1)
    {var country_score=1;}
    else {var country_score=0;}


    }

  2. #2
    Join Date
    Sep 2011
    Posts
    152

    Default

    could you please put space between the comparison operator like below:
    if (Country_ISO != null)

    let me know if it does not work

  3. #3

    Default

    It didn't work :-(

    Do you have more ideas? Thanks.

  4. #4
    Join Date
    May 2014
    Posts
    358

    Default

    Because the equality operator should be ==, not =.

  5. #5
    Join Date
    May 2016
    Posts
    282

    Default

    The compare if it's equal operator is == not a single =, also, sometimes is different null than empty string, so I have had to use the isEmpty(column) function:
    if ( isEmpty(Country_ISO)) {

    ...

    }
    else {...}

    if (Country_ISO == null){...}
    I'm not an expert in java or javascript, so it's an try/error until it works
    OS: Ubuntu 16.04 64 bits
    Java: Openjdk 1.8.0_131
    Pentaho 6.1 CE

  6. #6

    Default

    Thank you Ana,
    can you please tell me the syntax for "is Not Empty".
    Also, comparison Country_ISO == Country_ISO_1 still doesn't work. It seems like it is not reading the values from the previous step at all.
    Is there any way I can make JS to read the content?


  7. #7
    Join Date
    Nov 2009
    Posts
    688

    Default

    Why not using "User Defined Java Expression" with expression: Country_ISO.equals(Country_ISO_1) ? 1 : 0
    When
    Country_ISO can be null then something like Country_ISO == null ? 3 : Country_ISO.equals(Country_ISO_1) ? 1 : 0
    Then result when Country_ISO = Country_ISO_1 -> 1
    Country_ISO <> Country_ISO_1 -> 0
    Country_ISO is null -> 3

    This step is much faster

  8. #8
    Join Date
    Nov 2013
    Posts
    382

    Default

    For those of us that are not Java experts, a simple Formula step is the answer:

    if(isna([country_iso]);if([country]=[country_1];1;0);if([country_iso]=[country_iso_1];1;0))


  9. #9
    Join Date
    Aug 2016
    Posts
    290

    Default

    Well you don't need to be a java expert to write a simple if-test (also the syntax and logic is almost identical to most other programming languages)

  10. #10
    Join Date
    Apr 2008
    Posts
    4,696

    Default

    I'm going to go completely different direction here...

    Use a Filter Rows step to pull those rows where Country_ISO is null. Send those rows to another Filter Rows step that checks if Country=Country_1. Pass those rows to an
    "Add Constants" step that sets "country_score" to 1. Those where Country<>Country_1 get passed to an "Add Constants" that sets "country_score" to 0.

    Continuing on from the first Filter Rows step, those rows where "Country_ISO is not null" get also passed to a Filter Rows step, checking for Country_ISO=Country_ISO_1, which then redirects to the appropriate "Set Country Score" steps.

    Modified JavaScript Values steps is *SLOW* Do what you can to avoid using it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Privacy Policy | Legal Notices | Safe Harbor Privacy Policy

Copyright © 2005 - 2019 Hitachi Vantara Corporation. All Rights Reserved.