5 Replies Latest reply on Oct 25, 2018 7:47 PM by Brendan Murray

    formatted output using mrl.

    Kaushik KM
      Share:|

      Hi ALL,

       

      is there an option to output in integer in specific format in mrl ?

      options in sprintf ? i was unable to see them in KB ref guide..
      for example:

       

      IF $var = 2;

      i always need this value to be in 4 digit ie., $result = 0002;

       

      if  $var = 12;

      $result = 0012;

       

      if $var = 123;

      $restult = 0123;

       

      if $var = 1234

      $result = 1234;

       

      if not in sprintf, then what other options we can use ?
      we can find string length and prefix zeros ?

        • 1. Re: formatted output using mrl.
          Brendan Murray

          Hi Kaushik,

           

          I have never tried it, but I believe you should be able to use a sprintf format that will give you leading zeroes for a decimal integer. Our documentation specifies that the sprintf function in MRL works like the sprintf function in C. I couldn't find a good doc page for sprintf in C, but this page for Perl shows a format string that adds leading zeroes to a decimal integer:

           

          # Format number with up to 8 leading zeroes

          $result = sprintf("%08d", $number);

           

          Perl's sprintf function also emulates the C sprintf function, so I would expect this to work in MRL. I repeat, though, that I have not tested this. Please let us know if it works.

           

          Regards,

           

          Brendan

          1 of 1 people found this helpful
          • 2. Re: formatted output using mrl.
            Kaushik KM

            Hi @Brendan Murray,

             

            First Thank you for looking in !! and sorry for late response,

            Yes i tried the formatted sprintf like below, this is just half of the mrl.

             

            $TMP has the value which needs to be converted to 4 digit.

            ===================================

            if (($SLEN >= 1) AND ($SLEN < 4))

                                    then{

                                    $INTVAL = stringtoint($TMP);

                                    $TMP4D = sprintf("%04d", $INTVAL);

                                    $EV.lcl_store_number = $TMP4D;

                                    }

                                    else{

                                    $EV.lcl_store_number = $TMP;

                                    ntadd($EV,'store number is already 4 digit');

                                    };

            ============================================

             

            Even though i converted the TMP to integer by using stringtoint function and performed the operation on $INTVAL, it was throwing below error when compiling.

             

            so i used the below logic to get the work done.

             

            =====================================================================

            if (($SLEN >= 1) AND ($SLEN < 4))

            then{

            if ($SLEN == '1')

            then{

            $EV.lcl_store_number = '000' || $TMP;

            ntadd($EV,'store number updated to 4 digit');

            };

            if ($SLEN == '2')

            then{

            $EV.lcl_store_number = '00' || $TMP;

            ntadd($EV,'store number updated to 4 digit');

            };

            if ($SLEN == '3')

            then{

            $EV.lcl_store_number = '0' || $TMP;

            ntadd($EV,'store number updated to 4 digit');

            };

            }

            else{

            $EV.lcl_store_number = $TMP;

            ntadd($EV,'store number is already 4 digit');

            };

             

            ===========================================================

            but this would be a bad idea if the number is suppose to be a big one.

             

            Thanks,

            Kaushik KM

             

             

             

            Just another question :

            Do we have some utility ? or software by which we can compile our mrls written ? a baroc compiler ?

            modifing an mrl, and then its very difficult to compile and restart the cell everytime to test a small thing too.

            Please advice.

            • 3. Re: formatted output using mrl.
              Brendan Murray

              Hi Kaushik,

               

              First, regarding your sprintf function, you need to adjust your syntax. The example I provided in my post was for Perl. I'm sorry if it misled you. The MRL syntax is different, which is why I provided a link to the BMC documentation for the MRL sprintf function. In MRL, the second argument of the sprintf function needs to be a LIST_OF ANY data type, even if the list contains only one element. So your test would need to look like this:

               

              $TMP4D = sprintf("%04d", [$INTVAL]);

               

              Please refer to the BMC documentation for the correct syntax for sprintf. It is still possible that the "%04d" format string is not valid in MRL. I tried a quick test in my lab environment and I could not get it to work. However, I don't consider this to be conclusive. It may be that my MRL syntax is still not correct. If I get some time, I will play around with it some more.

               

              Your workaround using if-then logic may work, but I would not do it this way. The if-then-else construct in MRL has a high performance cost and it's not necessary in this case. Here's the way I would do it in a simple refine rule:

               

              refine leading_zeroes:

              EVENT ($EV) where [$EV.mc_object_class == 'leading_zeroes']

              {

                   $ZEROES = '0000';

                   $SLEN = strlen($EV.mc_parameter_value);

                   $RESULT=substring($ZEROES,$SLEN) || $EV.mc_parameter_value;

                   $EV.msg = $EV.msg || 'Result: ' || $RESULT;

              }

              END

               

              In my example, the mc_parameter_value contains the number to be converted. mc_parameter_value is a string data type. I use the MRL substring/3 function to extract the number of zeroes I need from $ZEROES. The length of mc_parameter_value determines how many of the zeroes in $ZEROES are extracted and placed in front of the mc_parameter_value. I tested this rule in my lab, and it worked fine. Of course, it only works if mc_parameter_value is no longer than 4 digits.

               

              Regards,

               

              Brendan

              1 of 1 people found this helpful
              • 4. Re: formatted output using mrl.
                Kaushik KM

                Hi Brendan Murray

                 

                the sprintf actually worked, after I changed to the proper syntax.

                       

                  $SLEN=strlen($TMP);

                          if (($SLEN >= 1) AND ($SLEN < 4)

                                  then{

                                  $INTVAL = stringtoint($TMP);

                                  $EV.myslot = sprintf("%04d", [$INTVAL]);

                                   ntadd($EV,'store number updated to 4 digit');

                                   }

                                   else{

                                   $EV.myslot = $TMP;

                                   ntadd($EV,'store number is already 4 digit');

                                   };

                 

                Thanks for another logic, i can use that in other MRL.

                Regards,

                Kaushik KM

                • 5. Re: formatted output using mrl.
                  Brendan Murray

                  You're very welcome, Kaushik. I am glad to hear you got the sprintf function to work for your purpose. I learned something too!

                   

                  Regards,

                   

                  Brendan