web stats
How to split a long string into fixed-length segments - Mirth Community

Go Back   Mirth Community > Mirth Connect > Support

Reply
 
Thread Tools Display Modes
  #1  
Old 11-05-2010, 07:18 AM
lijun lijun is offline
OBX.1 Kenobi
 
Join Date: Jul 2010
Posts: 27
lijun is on a distinguished road
Default How to split a long string into fixed-length segments

Hi there,

Does anyone know how to split a long string into fixed length segments in Mirth JavaScript reader? I have tried this but it doesn't work.

var array = longString.split("(?<=\\G.{" + maxLength + "})");
for (var i = 0; i < array.length; i++)
{
.....//do something
}

Thank you.
Reply With Quote
  #2  
Old 11-05-2010, 10:50 AM
AlexToft AlexToft is offline
OBX.3 Kenobi
 
Join Date: Sep 2010
Location: Leeds, UK
Posts: 160
AlexToft is on a distinguished road
Default

How about something like this? Will split into chunks as defined by the chunkSize variable, and the last chunk will be whatever's left over.

Code:
var longstring = "aisdfjsioefjoisdjcoisdjcoijsdocijsoidcjsiocdoisjdcoisjdocisdocji";
var chunkSize = 6;
var loops = Math.round(longstring.length/chunkSize);
var myArray = new Array;

for (i=0; i<loops; i++)
{
myArray[i] = longstring.substr(i*chunkSize,chunkSize);
};
If you want to see what it's doing, set your debug level to INFO and use something like this:

Code:
for(i=0; i<myArray.length; i++)
{
logger.info("Element "+i+" is "+myArray[i]);
};
Reply With Quote
  #3  
Old 11-05-2010, 12:30 PM
lijun lijun is offline
OBX.1 Kenobi
 
Join Date: Jul 2010
Posts: 27
lijun is on a distinguished road
Default

Cool, that worked.

I got JavaScript's slice work too.. The code is like this:
Quote:
var maxLength = 100;
while (true)
{
var chunk = longstring.slice(0, maxLength);
if (longstring.length <= maxLength)
break;
else
longstring= longstring.slice(maxLength);
}
Thanks!
Reply With Quote
  #4  
Old 04-07-2015, 05:50 AM
henriquetomaz henriquetomaz is offline
OBX.1 Kenobi
 
Join Date: Dec 2014
Posts: 45
henriquetomaz is on a distinguished road
Red face Using slice() to extract file name from complete path name

Hi

I am trying to extract the file name from the complete path name I receive from a HL7 message.

The path name is like this:
\\FAP\SYST\PDF\V4_2015\04\07\00056529_00094509\000 90378.PDF

I want to get "00090378" and assign it to the variable "id_documento"

Here is my code (in Source Transformer - Type: JavaScript):

var path_laudo = msg['OBX']['OBX.17']['OBX.17.1'].toString();
var id_documento = $('path_laudo').slice(($('path_laudo').length)-12,($('path_laudo').length)-4);
channelMap.put('id_documento',id_documento);

I have read previous topics here about it but, however I didnīt get it working properly yet.

Thanks.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -8. The time now is 12:24 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2019, vBulletin Solutions, Inc.
Mirth Corporation