BookmarkSubscribe
Choose Language Hide Translation Bar
tova
Community Trekker

parsing a character of numbers that begins with 0 and keeping the initial zero

Hi,

 

I want to make a vector of values that come from a column in a datatable.  Some of those values start with the number zero.  When I use the parse command it truncates the zero from the beginning of the number.  The column is defined as a character column.  The script I am using is eval(parse(column(DTI, "bar_pen")[i])));  How can I do this and keep my initial zeros?

 

Thanks,

 

Tova

0 Kudos
1 ACCEPTED SOLUTION

Accepted Solutions
Craige_Hales
Staff (Retired)

Re: parsing a character of numbers that begins with 0 and keeping the initial zero

dt = New Table( "Untitled",
    Add Rows( 3 ),
    New Column( "Bar_Pen", Character, Set Values( {"0001", "9999", "0900"} ) )
);

list = dt:Bar_pen << getvalues;
show(list); // {"0001", "9999", "0900"};

v = dt:Bar_Pen[3];
show(v, parse(v), char(parse(v))); // "0900", 900, "900"

You might not need to do the parse that converts the character data to code (which is just a number, most likely.) Then you don't need to eval the parsed code, then you don't need to convert the evaluated answer (a number) back to a string. If the values need cleaning up because of data entry errors, the eval/parse may seem to be working but will fail if a mischievous user puts bad code in the table.

Craige
3 REPLIES 3
Highlighted
tova
Community Trekker

Re: parsing a character of numbers that begins with 0 and keeping the initial zero

this is what worked for me in the end:

Repeat("0", 4 - length(char(eval(parse(column(DTI, "Bar_Pen")[i]))))) || char(eval(parse(column(DTI, "Bar_Pen")[i])));

0 Kudos
Craige_Hales
Staff (Retired)

Re: parsing a character of numbers that begins with 0 and keeping the initial zero

dt = New Table( "Untitled",
    Add Rows( 3 ),
    New Column( "Bar_Pen", Character, Set Values( {"0001", "9999", "0900"} ) )
);

list = dt:Bar_pen << getvalues;
show(list); // {"0001", "9999", "0900"};

v = dt:Bar_Pen[3];
show(v, parse(v), char(parse(v))); // "0900", 900, "900"

You might not need to do the parse that converts the character data to code (which is just a number, most likely.) Then you don't need to eval the parsed code, then you don't need to convert the evaluated answer (a number) back to a string. If the values need cleaning up because of data entry errors, the eval/parse may seem to be working but will fail if a mischievous user puts bad code in the table.

Craige
tova
Community Trekker

Re: parsing a character of numbers that begins with 0 and keeping the initial zero

thanks, that is a much neater solution than the one i found...