› Clovertech Forums › Read Only Archives › Cloverleaf › Cloverleaf › TCL to chang CR/LF file to NL
Cloverleaf 5.6 passes the MSH and stops, if i smanually change the CR/LF file to NL the file passes properly.
This is one way to read in a file “as is” (aka “slurp”) in tcl.
set f [open $fileName r]
fconfigure $f -translation binary
set reports [read $f]
close $f
-- Max Drown (Infor)
Robert,
Are you trying to do this within an existing tps? Some simple changes may help you achieve this goal.
Yes, I am attepting to make this happen inside of a TPS.
the vendor of this data cannot use TCP/IP nor change the structure of the file(s). Since this will be an on going interface there will be many files dropped at different times.
How to read a directory only only process if files exist and change structure to NL or EOF.
why not a simple
string map
However if Cloverleaf is reading the file it wil treat CRLF as LF
For example you read in a Windows file with several records separated by CRLF
If you split into individual records you simply split on n. Tcl knows it is Windows and treats the rn as n
As someone else mentioned it would help to know what sort of data you are dealing with. Is this a Windows file processing on Unix or what?
This is windows file processing.
The file sent is a single result, each line is separated with a CR/LF, sorry if I was not more clear.
Then as I said, Tcl will treat the CRLF as if it were a LF
Robert,
I am on Unix and I have an interface where I process a Windows file of several records that I split into individual messages to send to Xlate.
At first, the segments were separated by CR, and I was splitting on CR, so everything was fine. Suddenly, I started seeing the same issue you are experiencing, and after investigation, I found that the vendor was being inconsistent. Some days, the segments would be separated by CR (MAC format); other days, they would be separated by CRLF (DOS format).
Instead of running an outside “dos2mac” conversion utility on the file when necessary, I resolved the issue in the Tcl script itself by adding a single line of code to remove any heading LF (caused by the split) from the segment if the file was in DOS format:
run {
# ‘run’ mode always has a MSGID; fetch and process it
keylget args MSGID mh
set msg [msgget $mh]
# Split the message on Carriage Return to get the segments in a list
set segmentList [split $msg “x0d”]
foreach segment $segmentList {
# Remove any heading/trailing whitespace from the segment.
# This is a precaution in case the HL7 message came in DOS format
set segment [string trim $segment]
This way, it didn’t matter if they sent CR or CRLF.
Perhaps something like that may be useful to you.
Here is where I am starting..
proc fix_cf_lf { args } {
keylget args MODE mode ;# Fetch mode
set dispList {} ;# Nothing to return
switch -exact — $mode {
start {
# Perform special init functions
# N.B.: there may or may not be a MSGID key in args
}
run {
# ‘run’ mode always has a MSGID; fetch and process it
proc msgType {event} {
switch -exact — $event {
ORU {
}
msgType $event
}}
}
time {
# Timer-based processing
# N.B.: there may or may not be a MSGID key in args
}
shutdown {
# Doing some clean-up work
}
}
return $dispList
}
Never get more that the message header..
The file is attached, I change the TCL to include CR/LF no change.
MSH|^~&|LAB|MET||92373198|20100803142459.0000-0400||ORU^R01|00000000000000715383|P|2.3
proc fix_cr_lf { args } {
[code]
proc fix_cr_lf { args } {
Rpbert,
There are several issues with the code as shown. You are splitting on CRLF; you should be splitting on CR only.
You are not getting the additional segments because you are not adding them back to the new message after cleaning them. Also the foreach loop does nothing because you have a complete set of brackets {} after the foreach.
The code should look more like this:
proc fix_cr_lf { args } {
keylget args MODE mode ;# Fetch mode
set dispList {} ;# Nothing to return
switch -exact — $mode {
start {
# Perform special init functions
# N.B.: there may or may not be a MSGID key in args
}
run {
# ‘run’ mode always has a MSGID; fetch and process it
keylget args MSGID mh
set msg [msgget $mh]
# Split the message on Carriage Return to get the segments in a list
set segmentList [split $msg “x0d”]
set newmsg {} ;# to contain the modified message
foreach segment $segmentList {
# Remove any heading/trailing whitespace from the segment.
# This is a precaution in case the HL7 message came in DOS format
set segment [string trim $segment]
# Add the trimmed segment to the new message
lappend newmsg $segment
}
# Build the new message with CR as the segment separator
set newmsg [join $newmsg “r”]
# Add the final terminator. HL7 message should end with newline
append newmsg “rn”
# Message handle for the new message
set newmh [msgcopy $mh]
msgset $newmh $newmsg
# Continue the new message
lappend dispList “CONTINUE $newmh”
# Kill the original message
lappend dispList “KILL $mh”
}
time {
# Timer-based processing
# N.B.: there may or may not be a MSGID key in args
}
shutdown {
# Doing some clean-up work
}
} ;# end switch
return $dispList
} ;# end proc
I hope this helps.
Thank you very much
Robert,
To resolve your issue, please make the two following corrections to the above code:
# Add the final terminator. HL7 message should end with newline
append newmsg “r”
Add a condition to the ‘lappend’ to exclude any blank lines from your incoming files.
# Add the trimmed segment to the new message
if {[string length $segment]} {
lappend newmsg $segment
}