Maha
Maha

Reputation: 19

Counting the leading 0's and printing them

I am in the scenario where I need to count the leading 0's of the variable and place them in another variable using 4GL code.

For an example if the variable i (integer) is '00000546' then

  1. I need to count them as it is 5
  2. place them in another variable b (character) like '00000' + '546'

Please advise me.

Upvotes: 0

Views: 2128

Answers (6)

AquaAlex
AquaAlex

Reputation: 380

Easiest way to count leading zero:

DEF VAR iNum AS INTEGER NO-UNDO.
DEF VAR cValue AS CHAR NO-UNDO.
DEF VAR iLeadZero AS INTEGER NO-UNDO.

ASSIGN 
  cValue = '00000546'
  iNum = INTEGER(cValue)
  iLeadZero = LENGTH(cValue) - LENGTH(STRING(iNum)). 

Can also be done as a function (Without Error Handling):

FUNCTION cntLeadZeros RETURNS INTEGER
    (INPUT pcValue AS CHARACTER):
    DEF VAR iNum AS INTEGER NO-UNDO.
    DEF VAR iLeadZero AS INTEGER NO-UNDO.

    ASSIGN 
      iNum = INTEGER(pcValue)
      iLeadZero = LENGTH(pcValue) - LENGTH(string(iNum)). 

    RETURN iLeadZero.
END FUNCTION. /* cntLeadZeros */

Function with error handling, so if people pass thru a non integer value.

FUNCTION cntLeadZeros2 RETURNS INTEGER
    (INPUT pcValue AS CHARACTER):
    DEF VAR iNum AS INTEGER NO-UNDO.
    DEF VAR iLeadZero AS INTEGER NO-UNDO.

    ASSIGN 
      iNum = INTEGER(pcValue)
      iLeadZero = LENGTH(pcValue) - LENGTH(string(iNum)). 

    IF ERROR-STATUS:ERROR THEN
        RETURN ?.

    RETURN iLeadZero.
END FUNCTION. /* cntLeadZeros2 */ 

Upvotes: 2

Marcelo
Marcelo

Reputation: 704

Integer data type don't have leading zeros, so you don't need to count zeros. You can do it simply using STRING function like the example below. In this example below, I'm considering that your string has a fixed length.

DEFINE VARIABLE i-test  AS INTEGER INITIAL 546.
DEFINE VARIABLE c-test  AS CHARACTER.

ASSIGN c-test = STRING(i-test,"99999999").

MESSAGE c-test VIEW-AS ALERT-BOX.

Message will return: 00000546

Upvotes: 0

RonaldB
RonaldB

Reputation: 1200

OK, so i is an integer. The format of i needs to be determined, used to create a default string, then split it in zeros and non-zeros.

How about this:

DEFINE VARIABLE i AS INTEGER     NO-UNDO
    FORMAT "9999999999".

DEFINE VARIABLE vcINoLeading AS CHARACTER   NO-UNDO.
DEFINE VARIABLE vcIDefault AS CHARACTER   NO-UNDO.
DEFINE VARIABLE vcIZeros AS CHARACTER   NO-UNDO.

DEFINE FRAME bogus
    i.

i = 1234.

vcIDefault = STRING(i,i:FORMAT).
vcINoLeading = STRING(i).
vciZeros = REPLACE(vcIDefault,vcINoLeading,"").

MESSAGE vcIDefault SKIP vcINoLeading SKIP vciZeros VIEW-AS ALERT-BOX.

vcINoLeading has just the non-zero numbers, and vciZeros has all the zeros.

Upvotes: 1

Arno van der Ende
Arno van der Ende

Reputation: 682

I don't see why you want to assable the new variable like you describe, but you possible have a good reason. Perhaps you can explain more?

Anyhow, this is how I should do it:

DEFINE VARIABLE a  AS CHARACTER NO-UNDO.
DEFINE VARIABLE nr AS INTEGER   NO-UNDO.
DEFINE VARIABLE b  AS CHARACTER NO-UNDO.

a = STRING(546,"99999999").
nr = LENGTH(a) - LENGTH(LEFT-TRIM(a,"0")).
b = FILL("0",nr) + LEFT-TRIM(a,"0").

MESSAGE a SKIP nr SKIP b
    VIEW-AS ALERT-BOX INFO BUTTONS OK.

Upvotes: 0

Paul Krikke
Paul Krikke

Reputation: 86

The only tricky part in the question is where he mentions that "i" is an integer. Therefore, the leading zeros must come from the format of "i" as opposed to being part of a string. Thankfully, we can check the format of "i" and use that to determine how many leading zeros are being displayed.

DEFINE VARIABLE i      AS INTEGER   FORMAT '9999999999' NO-UNDO.
DEFINE VARIABLE cZeros AS CHARACTER FORMAT 'X(9)'       NO-UNDO.

DEFINE FRAME iFrame
    i.

i = 6123455.

cZeros = FILL('0',LENGTH(i:FORMAT) - LENGTH(STRING(i))).

DISPLAY
    i
    cZeros
WITH FRAME iFrame.

Upvotes: 0

TerryB
TerryB

Reputation: 629

Here is a way it could be done, loop through the characters:

def var a as char no-undo.
def var b as char no-undo.
def var c as char no-undo.
def var v as int  no-undo.

assign a = '00000546'.

do v = 1 to length(a):

   assign c = substring(a,v,1).

   if c ne "0" then leave.

end.

assign b = substring(a,v).

disp fill("0", v - 1) b.

Upvotes: 0

Related Questions