Strip or add leading zeros
Verified
Added by iNTERFACEWARE
Simple functions to strip or add leading zeros
Source Code
-- following best practice by putting local -- functions first before main() local function addZeros(Id, length) Id = string.rep('0', length-#Id)..Id return Id end local function removeZeros(Id) while true do if Id:sub(1,1) == '0' then Id = Id:sub(2) else break end end return Id end function main() local Id1 = removeZeros('0004589787') local Id2 = addZeros('4589787', 10) trace(Id1) trace(Id2) end
Description
Simple functions to strip or add leading zeros
Usage Details
This code uses two functions removeZeros()
and addZeros()
to strip or add leading zeros from a string.
How to use this code:
- Simply paste the code into any Lua script component
- Play around with it to see how it works, try changing the strings
- See if you can modify it work with numbers
Hint: Try usingtonumber()
andtostring()
or:S()
More Information
Added by iNTERFACEWARE
A very simple way to run all your code in protected mode with pcall() and log errors instead of stopping
Added by iNTERFACEWARE
How to prevent escaping of special characters like "&" in an HL7 message
Added by iNTERFACEWARE
How to process a batch of HL7 messages by removing the batch header and splitting it into separate messages.
Added by iNTERFACEWARE
Query a database to identify patient in incoming HL7 messages. If a patient is not found return an ACK with a "Patient does not exist" message.
Added by iNTERFACEWARE
Demonstrates the best way to read an empty XML TEXT element, by using the text(' ') function from the xml.lua module
Added by iNTERFACEWARE
Retries a a custom ACK using the retry.lua module
Added by iNTERFACEWARE
This code checks EDI (XML) data for some common errors, you can use this approach for pre-processing messages to clean them up
Added by iNTERFACEWARE
Use smtp.send() to send an email from your Lua code.
Added by iNTERFACEWARE
Demonstrates the best way to set a XML node to a space character, by using setText(' ') from the xml.lua module
Added by iNTERFACEWARE
How to transform part of an HL7 message (similar principles apply to other message types)
Added by iNTERFACEWARE
How to update a database using data from an HL7 message (similar principles apply to other message types)
Added by iNTERFACEWARE
How to handle inserts and updates for a table using an autoincrementing ID
Added by iNTERFACEWARE
How to generate an HL7 message with non-standard delimiters and/or escaping, using serialize.lua
Added by iNTERFACEWARE
Shows the correct way to add and update TEXT (and other) fields, as well as what not to do
Added by iNTERFACEWARE
How to use an external command program to zip up a folder containing multiple files