This topic contains 9 replies, has 4 voices, and was last updated by  Eliot Muir 2 years, 7 months ago.

Split text function given at interfaceware website breaks when text is large

  • Splitting a report into multiple fields;
    I am not very well versed with lua . I found this piece of code here on your website:
    http://help.interfaceware.com/kb/1079

    It does work for couple of lines but when the text chunk is very large, it breaks with error:

    Call stack has exceed maximum of depth of 100

    giving this error in findWord() function and message remains stuck in Iguana queue. Any help on this?

    Thanks

    The function could be re-written not to use recursion or you could search the net and find another implementation. There are quite a few out there. What are you doing with it?

    string.split is a short list of functions we are planning to roll into the core of Iguana itself – it’s something which gets used a lot in practice. Our re-write will most likely be in C – we have a few routines which would do the trick without much drama.

    string.split is a short list of functions we are planning to roll into the core of Iguana itself

    Did my suggestions from this weekend make the cut? 😉

    Jeff Drumm ◊ VP and COO ◊ HICG, LLC. ◊ http://www.hicgrp.com

    LOL 🙂 Thanks Eliot and Jeff. But Can I have something to get out of this problem at the moment? or may be if it’s possible to increase number of allowed recursive calls/stack size in Iguana?

    I think we need a little more information, Sim. What are you trying to split, and what are the rules to follow for splitting it? Is there a delimiter to split on, or are you just trying to break a large body of unformatted text into individual lines?

    Jeff Drumm ◊ VP and COO ◊ HICG, LLC. ◊ http://www.hicgrp.com

    I am trying to achieve exactly the same thing as in the split text example i.e Break large chunks of text into individual lines of 80 characters each .
    In the example given above, It breaks the text into lines of 80 characters(80 is the limit) and stores each of them in table called ‘rs’.
    So, ultimately what we get is

    Input: Large chunk of text

    rs[1] = ‘line 1 of 80 characters’
    rs[2] = ‘line 2 of 80 characters’

    and so on till the end of text chunk which was passed as input to split text function. and the words should not break in middle while marking the end of a line at 80 characters.

    🙂
    Please let me know if It was not clear

    This is a rewrite of the example without recursion. Be aware that it is not thoroughly tested.

    require('stringutil')
    require('node')
    
    local MAXLEN = 80 -- we wish to have maximum of 80 characters per line
    
    function main(Data)   
       local In,Name = hl7.parse{vmd='Untitled.vmd',data=Data}
       local Out = hl7.message{vmd='Untitled.vmd',name=Name}
       
       Out:mapTree(In)
       -- in this example we have only 1st repetition of Observation, with very long OBX-5
       Out.Observation[1].OBX:remove(1) 
       
       local RS = splitText(In.Observation[1].OBX[1][5]:S(),MAXLEN)
       
       for i=1,#RS do   
          Out.Observation[1].OBX[i][1] = i
          Out.Observation[1].OBX[i][2] = 'TX'
          Out.Observation[1].OBX[i][3][1] = In.Observation[1].OBX[1][3][1]
          Out.Observation[1].OBX[i][5][1] = RS[i]
       end
       
       queue.push(Out:S())
    end
    
    function splitText(s,maxLen)
       local rs ={}
       local start = 1
       local i = 1
    
       while true do
          local pos = start + maxLen
          local ct
          ct = s:find('[ \t]', maxLen)
          if ct == pos or ct == pos-1 then
             rs[i] = s:sub(start, start + maxLen - 1)
             start = start + maxLen
             i = i + 1
          else
             local cnt = s:sub(start, start + maxLen -1):reverse():find('[ \t]')
             if cnt == nil then cnt = 0 end -- if last rep doesn't find a space
             if #s:sub(start) < = maxLen then -- catch last rep           
                rs[i] = s:sub(start)
                break -- exit loop on last rep
             else
                rs[i] = s:sub(start, start + maxLen - cnt)
                start = start + maxLen - cnt + 1
             end
             i = i + 1
          end
       end
       return rs
    end
    

    Sim who are you doing this work for?

    @Julian muir: It was really an interesting implementation and worked just perfect:)
    Thanks a ton

    @jeff yes probably going to put some pcre compatible string functions in.

Tagged: 

You must be logged in to reply to this topic.