Clovertech
› Clovertech Forums › Read Only Archives › Cloverleaf › Tcl Library › re-order obx segs last to first using tcl
Looking to reverse order of obx segments using tcl without the lreverse command. Field values do not need any manipulation. Just each segment moved. Thank you!
You could try the tcl lsort command with the -decreasing command.
http://www.tcl.tk/man/tcl8.0/TclCmd/lsort.htm
-- Max Drown (Infor)
Here’s a simple example.
lappend obx_segments 1||||5||| 2||||5||| 3||||5||| 4||||5||| 5||||5||| 6||||5||| 7||||5||| 8||||5||| 9||||5||| 10||||5|||
puts $obx_segments
set obx_segments [lsort -decreasing -dictionary $obx_segments]
Output:
1||||5||| 2||||5||| 3||||5||| 4||||5||| 5||||5||| 6||||5||| 7||||5||| 8||||5||| 9||||5||| 10||||5||| 10||||5||| 9||||5||| 8||||5||| 7||||5||| 6||||5||| 5||||5||| 4||||5||| 3||||5||| 2||||5||| 1||||5|||
Simple
Assume $SEGMENTS is a list of all HL7 segments
set revLoc [lsort -integer -decreasing
[lsearch -all -regexp $SEGMENTS {^OBX}]]
set obxLst [lsearch -all -inline -regexp $SEGMENTS {^OBX}]
foreach loc $revLoc OBX $obxLst {
set SEGMENTS [lreplace $SEGMENTS $loc $loc $OBX]
}