comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Re: unformatted strings (reading)
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Return to the default flat view Create a new topic Submit Reply
Re: unformatted strings (reading) [message #5720 is a reply to message #5719] Sat, 03 February 1996 00:00 Go to previous message
agraps is currently offline  agraps
Messages: 35
Registered: September 1994
Member
immel@gi.alaska.edu (Thomas Immel) writes:


> Here's a question for anyone who reads unformatted strings.
> Is there a way to read a whole ,say 10 character, string at once
> in IDL? Here's what is happening to me.

[example deleted]

> 's' !?!? Just the first character. If I define bonk to be two spaces or three
> spaces then i get 'st' or 'stu' back. I know it's cause each character is 2
> bytes or something like that. Is there any way, other than defining bonk as 13
> blank spaces before reading,
> that I can read this string? Any bright ideas would be appreciated!

You could parse the string on delimiters like spaces, commas, tabs
etc. I took your example and modified it a little to show this. If
your string has just one field, just make your input string array to
be one element long (instead of 3 in the example below).

There's probably a more elegant way of doing this, but I've used the
below routine alot, and it's a good workhorse. You can easily modify
it to parse on different delimiters than spaces too.

Amara



;*********************************************************** *****************
PRO EXTRACT,A,S,INDEX
;Purpose: This subroutine takes a long string with values separated by
;blanks and extracts the values between the blanks and puts the values into
;the array 'S'. The routine
;automatically figures out how many fields are between the blanks as a check.
;INPUT: A: the long string (a line of data)
; S: a string array already formatted with fields
;OUTPUT: S the array with the values inserted
; INDEX: the number of fields in this line
;----------------------------------------------------------- ------------------
;Amara Graps 5-16-90
;----------------------------------------------------------- ------------------
lstring = strlen(a) ;length of input string
remainder = a
pos = strpos(remainder,' ',0) ;parse on the delimiter ' '
index = 0
while pos ne -1 do begin
remainder = strtrim(remainder,2)
pos = strpos(remainder,' ',0)
if pos ne -1 then begin
s(index) = strmid(remainder,0,pos+1)
remainder = strmid(remainder,pos,lstring)
end ;if
index = index + 1
end ;while

case 1 of
index gt 0: s(index-1) = strtrim(remainder,2)
index eq 0: s(index) = strtrim(remainder,2)
else:
endcase
end
;*********************************************************** ******************
;----------------------------------------------------------- --------
;IDL program: stringtest.pro
;Unformatted string read and write test by Amara Graps, 3 Feb 1996
;----------------------------------------------------------- --------

;Write the string
get_lun,unit
openw, unit, 'silly.dat'
stumblestring = 'stumblefloppy'+' '+'stumblefloppy'+ ' '+'stumblefloppy'
writeu, unit, stumblestring
close, unit
free_lun, unit

;Read the string
len_stumblestr = strlen(stumblestring)
format = '($'+',A'+string(len_stumblestr)+')'
bonk = string('',format) ;initialize line length to be len_stumblestr
get_lun,unit
openr, unit, 'silly.dat'
readu, unit, bonk
close, unit
free_lun, unit

help, bonk ;Look at bonk

;Parse the string
bonk_array = strarr(3) ;Set up bonk_array to be 3 string fields
extract, bonk, bonk_array, index

;Print the strings
help, bonk_array ;look at the bonk array
print, bonk_array(0)
print, bonk_array(1)
print, bonk_array(2)

end ; program stringtest.pro
;*********************************************************** ****************




--

************************************************************ ***********
Amara Graps email: agraps@netcom.com
Computational Physics vita: finger agraps@best.com
Multiplex Answers URL: http://www.amara.com/
************************************************************ ***********
"I see the correlation, but just because I work for the federal government
doesn't mean I'm an expert on cockroaches." --Agent Mulder (The X-Files)
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Previous Topic: LINKIMAGE under hpux
Next Topic: Re: Scientific Notation on Log Plots

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 19:29:16 PDT 2025

Total time taken to generate the page: 0.00371 seconds