-
I've been working on an issue that I can't seem to figure out. My ultimate goal with this javascript transformer is to look at each OBX segment, and if the value in OBX.5.1 has more than 95 characters, split it up into mutliple OBX segments, each with around 95 characters, without any words being split. To do this, I'm using a for each (seg in msg..OBX) loop. Then, after identifying if the OBX segment has 95 or more characters, I'm creating an array using .split() on the OBX.5 string. After that I'm going through building out a newString from the array until it hits 95 characters, at which point it creates a new OBX and creates another new string, etc till it's successfully split out the OBX segment. It works great for the first OBX segment that has over 95 characters. However, the issue I'm running into is that when it gets to the next OBX segment with over 95 characters, it can't overwrite the array, so that array just stays empty instead of repopulating with the .split() from the current OBX segment. I've tried creating the array empty, then using push, splice, and unshift to populate the array with .split(), but it just puts the entire .split() into lineArray[0], instead of filling out the entire array. So I tried do const lineArray = [], then var strArray = seg['OBX.5']['OBX.5.1'].split(" "). From there I tried using a for loop to take each object in the strArray, and populate it individually into the lineArray. But when I had it map out the lineArray results after it just listed each value as undefinded. So I'm needing to find a way to use .split() to populate an array within a for each loop, and each time through the loop be able to replace the values in the array with the new .split(). Here's the version of the script that works for the first qualifying OBX segment, but not the rest: for each (seg in msg..OBX)
{
charLen = seg['OBX.5']['OBX.5.1'].toString().length
channelMap.put("charLen",charLen)
if (charLen >= 95) //checking if OBX segment is long enough that it needs to be split into multiple OBXs
{
x = Number(seg['OBX.1']['OBX.1.1']) //identifying the OBX segment that is currently being looked at
channelMap.put("x",x)
const lineArray = seg['OBX.5']['OBX.5.1'].split(" ")
channelMap.put("lineArrayLength",lineArray.length)
if (lineArray.length >0)
{
newStr = lineArray[0].toString()+" " //creates variable to build text to plug back into report, and puts the first array value in + space
lineArray = lineArray.shift() //deletes the array value that was just put into newStr
strL = newStr.length //checks length of newStr
while (strL < 95 && lineArray.length > 0) //this loop keeps adding the next word in the array to newStr till it's longer than 95 characters or it runs out of words in the array
{
newStr = newStr+lineArray[0].toString()+" "
strL = newStr.length
lineArray = lineArray.shift()
}//while (strL < 95 && lineArray.length > 0)
seg['OBX.5']['OBX.5.1'] = newStr //plugs newStr back into the report
while (lineArray.length > 0)
{
newStr = lineArray[0].toString()+" " //overwrites newStr with the next word in the array plus a space
lineArray = lineArray = lineArray.shift() //deletes the array value that was just put into newStr
strL = newStr.length //checks length of newStr
while (strL < 95 && lineArray.length > 0) //this loop keeps adding the next word in the array to newStr till it's longer than 95 characters or it runs out of words in the array
{
newStr = newStr+lineArray[0].toString()+" "
strL = newStr.length
lineArray = lineArray.shift()
}//while (strL < 95 && lineArray.length > 0)
//This section builds out a new OBX after the current OBX being looked at
var newSeg = createSegmentAfter("OBX", msg['OBX'][x-1])
msg['OBX'][x]['OBX.1']['OBX.1.1'] = x+1
msg['OBX'][x]['OBX.2']['OBX.2.1'] = "TX"
msg['OBX'][x]['OBX.3']['OBX.3.1'] = $('OBX3.1')
msg['OBX'][x]['OBX.3']['OBX.3.2'] = $('OBX3.2')
msg['OBX'][x]['OBX.5']['OBX.5.1'] = newStr //plugs newStr back into the report in the new OBX
msg['OBX'][x]['OBX.11']['OBX.11.1'] = $('OBX11')+"|"
x = x+1
//this for loop renumbers the OBX.1.1 setID value for each OBX segment in the message
var setID = 1
for each(seg in msg..OBX)
{
seg['OBX.1']['OBX.1.1']= setID++;
}//for each(seg in msg..OBX)
}//while (lineArray.length > 0)
}//if (lineArray.length >0)
channelMap.put("newStr",newStr)
channelMap.put("strL",strL)
channelMap.put("newSeg",newSeg)
channelMap.put("setID",setID)
}//if (charLen >= 95)
}//for each (seg in msg..OBX) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
What is the nature of the OBXs? Is this just a long report such that you can simply join all the OBXs in one long string in a variable, delete the current OBXs, and add them back in by creating new OBXs? |
Beta Was this translation helpful? Give feedback.
-
I was finally able to get this to work how I needed. You can see the logic below. What I ended up doing is, instead of creating an array within the script using const, I wrote the array to a variable connectorMap value of "lineArray"+x, where x is the OBX.1.1 value of the current OBX being looked at. I then ran into the issue where I couldn't use the .shift function to remove the first value in the array, so I had to scrap that. Instead, I set y = 0, then incremented that up by 1 each time so I was moving through each object in the array. I set it to stop when y = length of the array. There were a few other minor adjustments to the original logic, but what you see below is able to look at each OBX segment, and it's got more than 95 characters (including spaces) split it out to new OBX segments until every OBX segment in the report has right around 95 characters without splitting any words. The purpose was to make a 1 to 1 relationship between OBX and row in the report. for each (seg in msg..OBX)
{
charLen = seg['OBX.5']['OBX.5.1'].toString().length
channelMap.put("charLen",charLen)
if (charLen >= 95) //checking if OBX segment is long enough that it needs to be split into multiple OBXs
{
x = Number(seg['OBX.1']['OBX.1.1']) //identifying the OBX segment that is currently being looked at
z = Number(seg['OBX.1']['OBX.1.1'])
channelMap.put("x",x)
connectorMap.put("lineArray"+x, seg['OBX.5']['OBX.5.1'].split(" "))
channelMap.put("lineArrayLength",$('lineArray'+x).length)
if ($('lineArray'+x).length > 0)
{
y = 0
newStr = $('lineArray'+x)[y].toString()+" " //creates variable to build text to plug back into report, and puts the first array value in + space
strL = newStr.length //checks length of newStr
y += 1
while (strL < 95 && y < $('lineArray'+x).length) //this loop keeps adding the next word in the array to newStr till it's longer than 95 characters or it runs out of words in the array
{//1
channelMap.put("y"+y,y)
channelMap.put("value"+y,$('lineArray'+x)[y].toString())
newStr = newStr+$('lineArray'+x)[y].toString()+" "
strL = newStr.length
y += 1
}//while (strL < 95 && lineArray.length > 0)
seg['OBX.5']['OBX.5.1'] = newStr //plugs newStr back into the report
while (y < $('lineArray'+x).length)
{//2
newStr = $('lineArray'+x)[y].toString()+" " //overwrites newStr with the next word in the array plus a space
strL = newStr.length //checks length of newStr
y += 1
while (strL < 95 && y < $('lineArray'+x).length) //this loop keeps adding the next word in the array to newStr till it's longer than 95 characters or it runs out of words in the array
{//3
channelMap.put("y"+y,y)
channelMap.put("value"+y,$('lineArray'+x)[y].toString())
newStr = newStr+$('lineArray'+x)[y].toString()+" "
strL = newStr.length
y += 1
}//while (strL < 95 && lineArray.length > 0)
//This section builds out a new OBX after the current OBX being looked at
channelMap.put("z"+z,z)
var newSeg = createSegmentAfter("OBX", msg['OBX'][z-1])
msg['OBX'][z]['OBX.1']['OBX.1.1'] = z+1
msg['OBX'][z]['OBX.2']['OBX.2.1'] = "TX"
msg['OBX'][z]['OBX.3']['OBX.3.1'] = $('OBX3.1')
msg['OBX'][z]['OBX.3']['OBX.3.2'] = $('OBX3.2')
msg['OBX'][z]['OBX.5']['OBX.5.1'] = newStr //plugs newStr back into the report in the new OBX
msg['OBX'][z]['OBX.11']['OBX.11.1'] = $('OBX11')+"|"
z = z+1
//this for loop renumbers the OBX.1.1 setID value for each OBX segment in the message
var setID = 1
for each(seg in msg..OBX)
{
seg['OBX.1']['OBX.1.1']= setID++;
}//for each(seg in msg..OBX)
}//while (lineArray.length > 0)
}//if (lineArray.length >0)
channelMap.put("newStr",newStr)
channelMap.put("strL",strL)
channelMap.put("newSeg",newSeg)
channelMap.put("setID",setID)
}//if (charLen >= 95)
}//for each (seg in msg..OBX) |
Beta Was this translation helpful? Give feedback.
I was finally able to get this to work how I needed. You can see the logic below. What I ended up doing is, instead of creating an array within the script using const, I wrote the array to a variable connectorMap value of "lineArray"+x, where x is the OBX.1.1 value of the current OBX being looked at. I then ran into the issue where I couldn't use the .shift function to remove the first value in the array, so I had to scrap that. Instead, I set y = 0, then incremented that up by 1 each time so I was moving through each object in the array. I set it to stop when y = length of the array. There were a few other minor adjustments to the original logic, but what you see below is able to look at…