Sidebar

Trying to add a character in each OBX segment

0 votes
585 views
asked Jan 5, 2021 by david-m-6327 (240 points)
Due to a vendor requirement, I need to add a ~ symbol if the OBX segment will be greater than 255 characters. The interface then rmeoves this character (does not print it in the end). My thought is to put the ~ after each period (.) character to ensure it's there in longer segments, as they are part of a report, and vary in length. This is the suggestion given by the vendor stated earlier.

So my question is, how do I do this? I tried some of the suggested links and came up with this:

// Get a count of all OBX fields

var obxArray = message.GetAllNodes('OBX-5');

// Iterate through each

for (var i=0; i < obxArray.length; i++) {

    var obx5 = '' + obxArray[i];  // Get current value of the OBX-5 segment on the OBX line we're on

    message.setNode('OBX-5', StringUtils.replace(obx5, '.' , '.~' ));

}

 

The idea is that if OBX-5 had a sentence such as the following:

This is the sentence that would be contained in the mentioned field. Since we don't know how long it might be, we'd like to add the tilde after each period. That will ensure the length will not cause a problem, per the vendor's statement.

After would look like:

This is the sentence that would be contained in the mentioned field.~ Since we don't know how long it might be, we'd like to add the tilde after each period.~ That will ensure the length will not cause a problem, per the vendor's statement.~

This however is giving me rather strange results, as it is injecting the value of the last OBX-5 segment into the first OBX-5 segment, and replacing nothing. Since I'm treating the two characters I'm looking to replace as strings, I don't think I'm triggering a special function.

1 Answer

+1 vote
 
Best answer

Here is code to add a '~' every 94 characters:

/*
* Add line-feeds to all OBX-5 lines longer than 94 characters
*
* This script will cycle through each OBX segement in the message
* and add any needed repeat ('~') characters to lines longer
* than 94 characters
*/
var instances = message.getAllNodes('OBX-5');
for (var inst = 0 ; inst < instances.length ; inst++) {
   var lines = instances[inst].split("~");
   var value = '';
   for (var i=0 ; i < lines.length ; i++) {
      if (lines[i].length() > 94) {
         var line = lines[i];
         // split the line
         while (line.length() > 94) {
            // find the last space character prior to position 94
            var breakPos = 93;
            for (; breakPos >=0 ; breakPos--) {
               if (line.substring(breakPos, breakPos+1).equals(' ')) {
                  // we have found a space character
                  // so break out of the loop
                  break;
               }
            }
            if (breakPos === 0) {
               // we didn't find a space going backwards so now
               // try going forward and find the next space
               breakPos = 94;
               for (; breakPos < line.length() ; breakPos++) {
                  if (line.substring(breakPos, breakPos+1).equals(' ')) {
                     // we have found a space character
                     // so break out of the loop
                     break;
                  }
               }
            }
            value += '~' + line.substring(0, breakPos);
            
            // now remove the portion of the line that we just processed
            // and proceed with the loop
            if (breakPos < line.length()-1) {
               line = line.substring(breakPos+1);
            } else {
               // we are at the end of the line
               line = '';
            }
         }
         if (line.length() > 0) {
            // add the last line segment
            value += '~' + line;
         }
      } else {
         value += '~' + lines[i];
      }
   }
   // remove the leading '~'
   value = value.substring(1);
   message.setNode('OBX['+ (inst+1) + ']-5', value);

 

answered Jan 5, 2021 by brandon-w-8204 (33,270 points)
selected Jan 5, 2021 by david-m-6327
commented Jan 5, 2021 by david-m-6327 (240 points)
Thank you for this answer to begin with. I am running into an error "Node not found: OBX[18]-5" (line 59). That's the very last line (message.setNode('OBX['+ (inst+1) + ']-5', value);

So I'm trying to follow the logic behind the code. The comment mentions that it removes the leading ~, but if I comment out the last two lines, it still doesn't put in any ~ symbols. If i'm following the code correctly, even without those last two lines it should give ~ characters in.
commented Jan 5, 2021 by brandon-w-8204 (33,270 points)
I have updated the answer. The line following line was changed:
var instances = source.getAllNodes('OBX-5');
To this:
var instances = message.getAllNodes('OBX-5');

That should fix the error you are getting.
commented Jan 5, 2021 by david-m-6327 (240 points)
Perfect. This does what I need it to. Now if you can fix the vendor, as now it's in the correct format (per them) yet still isn't working right. Ugh!
...