Home   Package List   Routine Alphabetical List   Global Alphabetical List   FileMan Files List   FileMan Sub-Files List   Package Component Lists   Package-Namespace Mapping  
Routine: HDISVM05

HDISVM05.m

Go to the documentation of this file.
  1. HDISVM05 ;BPFO/JRP - PARSE DELIMITTED TEXT;6/26/2007
  1. ;;1.0;HEALTH DATA & INFORMATICS;**7**;Feb 22, 2005;Build 33
  1. ;
  1. ;**
  1. ;** The following code was copied from SCMSVUT5, which I
  1. ;** originally wrote in July of 2002. It was copied to
  1. ;** prevent the dependancy on the Scheduling package, which
  1. ;** may not exist on all systems.
  1. ;**
  1. ;** It has also been modified to handle fields that contain
  1. ;** the separator character. These fields are surrounded by
  1. ;** quotation marks (ex: ...^"Field ^ With ^ Separator"^...).
  1. ;** This was done because the original parser was written for
  1. ;** parsing HL7 messages and this parser is meant for parsing
  1. ;** delimitted text (ie spreadsheets).
  1. ;**
  1. ;
  1. PARSE(INARR,OUTARR,SEP,SUB,MAX) ;Parse array into individual fields
  1. ;Input : INARR - Array containing data to parse (full global ref)
  1. ; INARR = First 245 characters of data
  1. ; INARR(1..n) = Continuation nodes
  1. ; OR
  1. ; INARR(x) = First 245 characters of data
  1. ; INARR(x,1..n) = Continuation nodes
  1. ; OUTARR - Array to put parsed data into (full global ref)
  1. ; SEP - Field separator (defaults to ^) (1 character)
  1. ; SUB - Starting subscript of OUTARR (defaults to 0)
  1. ; MAX - Maximum length of output node (defaults to 245)
  1. ;Output : None
  1. ; OUTARR(SUB) = First piece (MAX characters)
  1. ; OUTARR(SUB,1..n) = Continuation nodes
  1. ; OUTARR(SUB+X) = Xth piece (MAX characters)
  1. ; OUTARR(SUB+X,1..n) = Continuation nodes
  1. ;Notes : OUTARR is initialized (KILLed) on entry
  1. ; : Assumes that INARR and OUTARR are defined and valid
  1. ;
  1. ;Declare variables
  1. NEW NODE,STOP,DATA,INFO,FLD,SEPCNT,CN,OUT,TMP,ROOT,OUTNODE
  1. NEW QUOTE,HASQ,ADDSEP
  1. KILL @OUTARR
  1. SET SEP=$GET(SEP) SET SEP=$EXTRACT(SEP,1) SET:SEP="" SEP="^"
  1. SET SUB=+$GET(SUB)
  1. SET MAX=+$GET(MAX) SET:'MAX MAX=245
  1. SET NODE=INARR
  1. SET INFO=$GET(@NODE)
  1. SET ROOT=$$OREF^DILF(INARR)
  1. SET FLD=1
  1. SET SEPCNT=$LENGTH(INFO,SEP)
  1. SET STOP=0
  1. SET OUTNODE=$NAME(@OUTARR@(SUB))
  1. SET QUOTE=$CHAR(34)
  1. SET HASQ=0
  1. SET ADDSEP=0
  1. SET CN=0
  1. ;Loop through all columns in all nodes
  1. FOR SET DATA=$PIECE(INFO,SEP,FLD) DO QUIT:STOP
  1. .;Check for data in double quotes
  1. .IF (DATA[QUOTE) IF (($LENGTH(DATA,QUOTE)-1)#2) DO
  1. ..;Check for leading double quote
  1. ..IF ('HASQ) DO
  1. ...;Separator on next node (don't append now in case it isn't)
  1. ...IF (FLD=SEPCNT) SET ADDSEP=1 QUIT
  1. ...;Append separator
  1. ...SET DATA=DATA_SEP
  1. ...SET ADDSEP=0
  1. ...QUIT
  1. ..SET HASQ='HASQ
  1. ..QUIT
  1. .;Need to append separator when the leading quotation mark was
  1. .;on the previous node AND the next separator is encounterd
  1. .IF (ADDSEP) IF (HASQ) IF (DATA'=QUOTE) IF (INFO[SEP) IF (FLD'=SEPCNT) DO
  1. ..SET DATA=DATA_SEP
  1. ..SET ADDSEP=0
  1. ..QUIT
  1. .;End of line - store in output global
  1. .IF FLD=SEPCNT DO QUIT
  1. ..DO ADDNODE
  1. ..;Get next line of data from input global
  1. ..SET NODE=$QUERY(@NODE)
  1. ..;No more data - stop outer loop
  1. ..IF (NODE="")!(NODE'[ROOT) SET STOP=1 QUIT
  1. ..;Set text and column variables
  1. ..SET INFO=$GET(@NODE)
  1. ..SET SEPCNT=$LENGTH(INFO,SEP)
  1. ..SET FLD=1
  1. ..QUIT
  1. .;Add column to output global
  1. .DO ADDNODE
  1. .;Increment subscript if not in middle of double quotes
  1. .IF ('HASQ) SET SUB=SUB+1
  1. .;Set next storage node to use
  1. .SET OUTNODE=$NAME(@OUTARR@(SUB))
  1. .;Increment column number
  1. .SET FLD=FLD+1
  1. .;Reset continuation node number
  1. .SET CN=0
  1. .QUIT
  1. QUIT
  1. ADDNODE ;Used by PARSE to add data to output node
  1. ;Get currently stored column value
  1. SET TMP=$GET(@OUTNODE)
  1. ;Length of node won't go over max value - append
  1. IF ($LENGTH(TMP)+$LENGTH(DATA))<(MAX+1) SET @OUTNODE=TMP_DATA QUIT
  1. ;Append as much as stored column value can take
  1. SET @OUTNODE=TMP_$EXTRACT(DATA,1,(MAX-$LENGTH(TMP)))
  1. ;Increment continuation node number
  1. SET CN=CN+1
  1. ;Recursively call self to store remaining text
  1. SET DATA=$EXTRACT(DATA,(MAX-$LENGTH(TMP)+1),$LENGTH(DATA))
  1. SET OUTNODE=$NAME(@OUTARR@(SUB,CN))
  1. IF DATA'="" DO ADDNODE
  1. QUIT