Generating a right-justified numeric sequence

Hi again,

I’m now trying to generate a file with test data. I have set up a graph with a DataGenerator component. I want the first field of the output file to be a numerical sequence of 10 digits. It is working but not really as numerical sequence, I mean, it is left justified and not right.

For example, it is generating something like this:

1 secondfield
2 secondfield
3 secondfield

10 secondfield
11 secondfield
12 secondfield

99 secondfield
100 secondfield

This is not what I would expect from a numerical sequence but something like the following (using underscore instead of space because the forum does not respect initial spaces):

_________1secondfield
_________2secondfield
_________3secondfield

________10secondfield
________11secondfield
________12secondfield

________99secondfield
_______100secondfield

And it should be still better if it was like this:

0000000001secondfield
0000000002secondfield
0000000003secondfield

0000000010secondfield
0000000011secondfield
0000000012secondfield

0000000099secondfield
0000000100secondfield

Is it possible? Thanks in advance.

Hi dalonso,
I would suggest to use Reformat component to sort it out. Its transformation could look as follows:
$0.field1 := right(’ ’ + $0.field1, 10);
$0.field2 := $0.field2;

Where the field1 is an integer on input and string on output
Field2 is string on both sides.

Number of spaces I used on first row of transformation depends on your needs(lenght of your sequence).

I hope it will help you.

Best regards,
Tomas

Hi Thomas,

thanks, it’s a good solution and I will be using it.

Though performance wise I hoped to be able to save one component in the graph.

I’m going to work with big, big files, so using the Reformat component means traversing all the file, for reformatting all the records, when it could have been done in the origin. From my point of view, it’s a pity if the sequence does not honour the format of the receiving field (all integers should be rigth justified in my oppinion).

I could maybe request this to the developers.

Best regards.

Unfortunately the solution pointed by Thomas was not working. Maybe, I missed something.

Anyway, I had to use a transform function but with a custom made padding function:

        function rightjustify (src, paddedlength, pad){
          string out_str = src;
          int src_len = length(src);
          int dest_len = paddedlength;
          int num_pad = dest_len - src_len;
          print_err('fdest_len=' + dest_len);
          print_err('src_len=' + src_len);
          for (dest_len;dest_len > src_len;dest_len--) {
            out_str = pad + out_str;
          }
          return out_str;  
        }
        function transform (){
          $CustomerCode := rightjustify($CustomerCode,10,'0');
      }

I write it here for reference. I pads a string, with any character (‘0’ in this example) right justifying it.

So for:
1
2
3
4
5

I get:
0000000001
0000000002
0000000003
0000000004
0000000005
Best regards.

Hi dalonso,

If you want to pad the field by a serie of 0-s from the left, you only need to set the Format attribute for this field to 0000000000.
This way, the numbers will be completed to 10 digits by 0-s from the left.
Click the desired field in the left pane of Metadata editor and set the Format attribute in the Field pane on the right side of this editor to the following string: 0000000000.
You will not need any Reformat and you will obtain field values in the form as you want.

Tomas Waller

However, if you want to complete the field by white spaces, you need Reformat with the following code:


function transform() {
string a;
string b;
int Length;

a=" ";
Length=length("" + $0.field1);

while (Length<10) {
	Length++;
	b=b+a;
}

b=b+$0.field1;

$0.field1 := b;
$0.field2 := $0.field2;
}

This way Reformat converts integer numbers to right-justified strings.

Tomas Waller

My god!! I cannot believe it was so easy!!!

Thanks a lot. Anyway, a learn a lot about coding functions.