; $Id: readword.pro,v 1.2 2002/01/14 20:59:26 amunter Exp $
function readword,ttemp,num

;to read the Nth word from a string. The words are seperated by "'" and ' '

;temp:the string
;num:the number of the  word in the string

temp=''

temp=' '+ttemp
l=strlen(temp)
n=0
flag=0
comp=' '    ;use ' ' as the seperation of words
length=0    ;length of the word
initial=0   ;starting position of the word

for i=0,l-1 do BEGIN
	IF (FLAG) THEN BEGIN

		;to tell a word like:"    'word'"
		
		IF (comp EQ ' ' AND strmid(temp,i,1) EQ "'" AND length EQ 0) $
		THEN BEGIN
			comp="'"
			initial=i 			
		ENDIF 

		;find the end of a word		
		IF (strmid(temp,i,1) EQ comp ) THEN BEGIN

			;is the ' ' or "'" before or after the word?

			IF (length NE 0) THEN BEGIN	
				IF (n EQ num) THEN goto,sendword ELSE BEGIN
					n=n+1 
					comp=' '
					initial=i+1
					length=0
				ENDELSE
			ENDIF ELSE initial=initial + 1
		ENDIF ELSE length=length +1
	ENDIF ELSE BEGIN
		IF (strmid(temp,i,1) EQ ' ') THEN BEGIN
			comp=' '
			FLAG=1
			initial=i+1
		ENDIF
		IF (strmid(temp,i,1) EQ "'") THEN BEGIN
			comp="'"
			FLAG=1
			initial=i+1
		ENDIF
	ENDELSE
endfor

if n ne num then length=0

sendword:str=strmid(temp,initial,length)
return,str
end



