Customer Portal

Generating a right-justified numeric sequence

Comments 6

  • Avatar
    trach
    0
    Comment actions Permalink
    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
  • Avatar
    dalonso
    0
    Comment actions Permalink
    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.
  • Avatar
    dalonso
    0
    Comment actions Permalink
    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.
  • Avatar
    twaller
    0
    Comment actions Permalink
    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
  • Avatar
    twaller
    0
    Comment actions Permalink
    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
  • Avatar
    dalonso
    0
    Comment actions Permalink
    My god!! I cannot believe it was so easy!!!

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

Please sign in to leave a comment.