VWUTIL ;WVEHR/Maury Pepper/Skip Ormsby- World VistA Utilities;7:32 PM  30 Jan 2008
 ;;WVEHR-1007;WORLD VISTA;*WVEHR1*;;WorldVistA 30-Jan-08
 ;
 ;Modified from FOIA VISTA,
 ;Copyright 2008 WorldVistA.  Licensed under the terms of the GNU
 ;General Public License See attached copy of the License.
 ;
 ;This program is free software; you can redistribute it and/or modify
 ;it under the terms of the GNU General Public License as published by
 ;the Free Software Foundation; either version 2 of the License, or
 ;(at your option) any later version.
 ;
 ;This program is distributed in the hope that it will be useful,
 ;but WITHOUT ANY WARRANTY; without even the implied warranty of
 ;MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 ;GNU General Public License for more details.
 ;
 ;You should have received a copy of the GNU General Public License along
 ;with this program; if not, write to the Free Software Foundation, Inc.,
 ;51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 ;
 Q
 ;
Q(V,D) ; Function to return $QUERY for variable V and direction D.
 ; Replacement for Reverse $Q Function
 ; 1/8/08 MLP
 ;This function can be called for $Query -- either forward or reverse.
 ;In place of $Q(V,D), use $$Q^ZDQ($NA(V),D)
 ;Note: the 2nd argument is optional.
 ;
 S D=+$G(D,1)
 Q:D=1 $Q(@V)         ;Forward $Q
 IF D'=-1 Q           ;Will cause error due to no argument.
 N S
TOP IF $QL(V)=0 Q ""     ;done if unsubscripted
BKU S S=$O(@V,-1)        ;backup to previous node on current level
 S V=$NA(@V,$QL(V)-1) ;remove last subscript
 IF S="" G DAT        ;go chk for data if backed up all the way
 S V=$NA(@V@(S))      ;add the subscript found when backing up.
 IF $D(@V)>9 S V=$NA(@V@("")) G BKU  ;if downpointer, descend and repeat
DAT IF $D(@V)#2=1 Q V    ;if a data node, return with current name
 G TOP
