Clarity

 View Only
  • 1.  Padding Integers with Leading Zeroes in GEL Script

    Posted Apr 24, 2019 03:10 PM

    Calling GEL and Java gurus, I'm neither and this is making me crazy.

     

    I'm creating a GEL script that parses a tab-delimited text file regarding product shipments, then builds a XOG file and imports it into a custom object.  So far, so good ... except for one thing I can't do ...

     

    My source data contains the shipment date's year (4 digits) in column 25 and the shipment date's month (1 or 2 digits, depending upon the month) in column 27.  My intention is to concatenate the two values into a 6-character YYYYMM value (examples: January 2019 = 201901, November 2018 = 201811, etc.).

     

    While debugging, I'm simply logging the values so I can see the results.  My code is:

     

    <gel:log>${row[25]}${row[27]}</gel:log>

     

    I've tried all sorts of ways to pad and  concatenate those strings, but I can't figure out how to: 1) zero-pad the month value so that 1 becomes 01, 2 becomes 02, etc., but 10, 11 and 12 remain 10, 11 and 12 (with no padding), and; concatenate the two values into a single string.

     

    Java zero-padding articles, haven't been helpful -- I can't figure out how to incorporate what I read in those articles into the GEL script and make it work. So my workaround is open the text file in Excel, add an additional column that uses the TEXT() formula to pad and concatenate the two strings, and re-save the TXT file with the extra column -- then just doing a straight read of that column to get my YYYYMM value, which is already transformed.  But I'd REALLY prefer to not have to do that extra maintenance on the source file every time.

     

    If anyone could provide a code sample of how to do this, you'd be my hero.



  • 2.  Re: Padding Integers with Leading Zeroes in GEL Script
    Best Answer

    Broadcom Employee
    Posted Apr 25, 2019 12:57 AM

    If you get "2019 1"(including blank at the front of month), you will be able to use replaceAll method.

     

    I run below gel script.

     

    ----------------------------------

    <gel:script
    xmlns:core="jelly:core"
    xmlns:gel="jelly:com.niku.union.gel.GELTagLibrary">

    <core:set var="year1" value="2019"/>
    <core:set var="month1" value=" 1"/>
    <core:set var="result1" value="${year1}${month1}"/>
    <gel:out>date is ${result1}</gel:out>

    </gel:script>

    ----------------------------------

    It returns below.

    >date is 2019 1

     

    I added replaceAll method like as below.

    ---------------------------------

    <gel:script
    xmlns:core="jelly:core"
    xmlns:gel="jelly:com.niku.union.gel.GELTagLibrary">

    <core:set var="year1" value="2019"/>
    <core:set var="month1" value=" 1"/>
    <core:set var="result1" value="${year1}${month1}"/>
    <gel:out>date is ${result1}</gel:out>

    <core:set var="result2" value="${result1.replaceAll(' ', '0')}"/>
    <gel:out>date is ${result2}</gel:out>

    </gel:script>

    ---------------------------------

     

    It returns below.

    >date is 2019 1
    >date is 201901

     

    If you get month as 1 number character, you will need to check length.

    For example, you get January as 1, not 01.

    If length is 1, then you need to concat "0" string at front of month.

     

    <gel:script
    xmlns:core="jelly:core"
    xmlns:gel="jelly:com.niku.union.gel.GELTagLibrary">

    <core:set var="year1" value="2019"/>
    <core:set var="month1" value="1"/>
    <gel:out>month is ${month1}</gel:out>
    <gel:out>date is ${year1}${month1}</gel:out>

    <core:set var="length1" value="${month1.length()}"/>
    <gel:out>length is ${length1}</gel:out>

    <core:if test="${length1==1}">
        <core:set var="zero1" value="0"/>
        <core:set var="month1" value="${zero1.concat(month1)}"/>
    </core:if>
    <gel:out>date is ${year1}${month1}</gel:out>

    </gel:script>

     

    It returns below.

    >month is 1
    >date is 20191
    >length is 1
    >date is 201901

     

    Regards,

    Shoichi



  • 3.  Re: Padding Integers with Leading Zeroes in GEL Script

    Posted Apr 25, 2019 12:22 PM

    Here is another way using substring.

     

    <?xml version="1.0" encoding="UTF-8"?>
    <gel:script xmlns:core="jelly:core" xmlns:gel="jelly:com.niku.union.gel.GELTagLibrary" >

         <core:set var="pad" value="0000000000" />
         <core:set var="year" value="2019" />
         <core:set var="month" value="4" />

         <gel:log>${pad.concat(year.trim()).substring(pad.concat(year.trim()).length() - 4)}</gel:log>
         <gel:log>${pad.concat(month.trim()).substring(pad.concat(month.trim()).length() - 2)}</gel:log>
         
         <core:set var="year" value="${pad.concat(year.trim()).substring(pad.concat(year.trim()).length() - 4)}" />
         <core:set var="month" value="${pad.concat(month.trim()).substring(pad.concat(month.trim()).length() - 2)}" />
         <gel:log>${year.concat(month)}</gel:log>

    </gel:script>

     

     

    Update to add the trim in case there was a leading space on the input.

     

    V/r,

    Gene