Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado
In article <4eule1$si1@news.alaska.edu> 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? Not in general. When written as standard binary, strings don't contain any information about their length, or markers between them. Consequently, IDL has no way of knowing how many bytes to read. However, in the XDR binary mode, stringlengths are stored along with the string, allowing something like the following: IDL> teststrings= ['This','is','a','set of','test strings. Okay?'] IDL> openw,1,'test.dat',/XDR IDL> writeu,1,teststrings IDL> close,1 IDL> openr,1,'test.dat',/XDR IDL> newstrings= strarr(5) ;define some strings, don't worry about their lengths IDL> readu,1,newstrings IDL> print,newstrings This is a set of test strings. Okay? Of course, this is only useful if you can make sure XDR is used in the first place (and if you had control over the writing you could just pad everything out to some known length). Otherwise you can try things like searching for spaces (like another poster suggested), but that could get messy if there are non-strings mixed in with the data. Good luck. Brian Jackel University of Western Ontario
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)
immel@gi.alaska.edu (Thomas Immel) wrote: Thanks for the info and email on this subject. The external data representation is just what I needed to know about. Thanks again Thomas Immel > 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. > 1)Open an unformatted idl file and write a silly string like 'stumblefloppy' > get_lun,unit > openw, unit, 'silly.dat' > writeu,unit,'stumblefloppy' > free_lun, unit > 2) Try to read the string > bonk=' ' ; this is the string variable that I read into > get_lun,unit > openr, unit, 'silly.dat' > readu, unit, bonk > 3) OK, I'm ready to print 'stumblefloppy'! > print,bonk > s > '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! > Thomas Immel > Immel@geewiz.gi.alaska.edu > P.S. Ken Knighton, whoever he is, is RIGHT ON!
immel@gi.alaska.edu (Thomas Immel) wrote: > > > 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. > > > > Thomas Immel > Immel@geewiz.gi.alaska.edu > If you know the length of the string you want to read, you can always do something like: openw, 5, 'test' writeu, 5, 'Whatever' free_lun, 5 openr, 5, 'test' s = string( replicate(32B, 8) ) ; String of 8 spaces readu, 5, s free_lun, 5 print, s Whatever