Javascript

Sort:
You are not authorized to post a reply.
Author
Messages
Jeff
New Member
Posts: 2
New Member

    I am new to IPA and javascripting but so I was wondering if anyone can help me on how to create a fixed length field for use in an Assign node.  I have a field that can vary in length that I need to include it in a flat file interface

    I have been able to use "addTrailingSpaces" but this wont work when the data in the field are different lengths. 

    Probably an easy solution but I cant seem to find anything that works...Thanks in advance for any assistance you can provide!

    brox07
    Basic Member
    Posts: 8
    Basic Member
      I wrote this real quick and haven't tested it but it should give you a decent starting point at least.

      var1 = '1234'; // this is the variable that you're passing in
      varLen = 8; // length that you want your variable to be
      for(i=var1.length;i   var2 = var1; // rename variable to preserve original data
        var2 += '0'; // add 0's
      }

      brox07
      Basic Member
      Posts: 8
      Basic Member
        Here's a resource if you want to learn more about JS.
        https://www.w3schools.com/js/default.asp
        Terry P
        Veteran Member
        Posts: 234
        Veteran Member
          In your start node create a variable called "vSpace". I also created one called "vEOF" with what they required for an end-of-file marker.

          Then using the ASSIGN node, add similar javascript commands for each field. The first addTrailingSpaces value sets the fixed length of the field, the last addTrailingSpaces puts in the number of spaces between each field

          addTrailingSpaces(Qry1_JOB_CODE,9,0) + addTrailingSpaces(vSpace,16,0) +
          addTrailingSpaces(Qry1_EXEMPT,1,0) + addTrailingSpaces(vSpace,15,0) +
          addTrailingSpaces(Qry1_DESCRIPTION,30,0) + addTrailingSpaces(vSpace,225,0) +
          vEOF
          Dave Curtis
          Veteran Member
          Posts: 136
          Veteran Member
            For fixed length field files I typically use SQL node to extract the data or manipulate the data because I feel like I have more control over the output with using a SQL node.

            There is a javascript command .padStart and .padEnd such as

            fielddata.padEnd(10);

            That would pad the field data to 10 (or what you set) and you can add an item to pad with if you wanted: fielddata.padEnd(10,x)

            However - I am not sure that function is included in the library within IPA.

            This is probably kind of a 'crude' way but I have done something like this
            Within the assign node build a javascript expression like this:

            var as = addTrailingSpaces(strYourFieldHere, 50, 0);
            var ss = substring(as, 0,10);
            strYourFieldHere = ss;

            So your first part you add spaces beyond any length you would expect that field to be. Then your second line uses the substring to cut it back down to the size you want. The result would be
            A field has data that is 5 characters long, after the first line it would be 55 characters long. Then the second line cuts it down to 10 characters long. Leaving you with the 5 characters plus 5 spaces.

            Crude but it works.
            Jeff
            New Member
            Posts: 2
            New Member
              Thanks everyone!  It turns out addTrailingSpaces will work...I had the wrong number of spaces in one of my Assigns...Ugh. 
              You are not authorized to post a reply.