Using a text list to populate a LIST() in IDL [message #87693] |
Sun, 23 February 2014 14:31  |
johnson.dustin.astro
Messages: 2 Registered: February 2014
|
Junior Member |
|
|
Hi,
I am writing a function/routine that makes use of a user provided list of strings. Right now, the user needs to edit the LIST(a,b,c,d) parameter. I was wondering if there is a way for me to have the routine reference a .txt file and use the contents to populate the list, rather than have the user edit the function.
Does anyone know how I would accomplish this?
Thanks!
|
|
|
Re: Using a text list to populate a LIST() in IDL [message #87707 is a reply to message #87693] |
Mon, 24 February 2014 09:20   |
Paul Van Delst[1]
Messages: 1157 Registered: April 2002
|
Senior Member |
|
|
I created a test file, "test_string.txt", that contains the following
(minus the "%<" lines):
-----%<-----
this is string one
and now string two
this little piggy cried wee wee wee...
the end
-----%<-----
Then the procedure:
-----%<-----
pro read_into_list, user_list
user_list = list()
openr, fid, "test_string.txt", /get_lun
cbuffer = ""
while (~ eof(fid)) do begin
readf, fid, cbuffer
user_list.add, cbuffer
endwhile
free_lun, fid
end
-----%<-----
Running the above in IDL:
IDL> .run read_into_list
% Compiled module: READ_INTO_LIST.
IDL> read_into_list, x
IDL> help, x
X LIST <ID=1 NELEMENTS=4>
IDL> for i=0, n_elements(x)-1 do help, x[i]
<Expression> STRING = 'this is string one'
<Expression> STRING = 'and now string two'
<Expression> STRING = 'this little piggy cried wee wee wee...'
<Expression> STRING = 'the end'
When I edit the file and run again I get
IDL> read_into_list, x
IDL> for i=0, n_elements(x)-1 do help, x[i]
<Expression> STRING = 'this is string one'
<Expression> STRING = 'and now string two'
<Expression> STRING = 'toss in a third string,'
<Expression> STRING = 'this little piggy cried wee wee wee...'
<Expression> STRING = 'the end'
<Expression> STRING = 'not really. This the the last line'
How's that?
cheers,
paulv
On 02/23/14 17:31, johnson.dustin.astro@gmail.com wrote:
> Hi,
>
>
> I am writing a function/routine that makes use of a user provided
> list
of strings. Right now, the user needs to edit the LIST(a,b,c,d)
parameter. I was wondering if there is a way for me to have the routine
reference a .txt file and use the contents to populate the list, rather
than have the user edit the function.
>
> Does anyone know how I would accomplish this?
>
> Thanks!
>
|
|
|
Re: Using a text list to populate a LIST() in IDL [message #87711 is a reply to message #87707] |
Mon, 24 February 2014 20:04   |
Dick Jackson
Messages: 347 Registered: August 1998
|
Senior Member |
|
|
Hi folks,
If an extra copy of the strings will fit in memory, this might be easier:
f = 'test_string.txt'
strs = StrArr(File_Lines(f))
OpenR, unit, f, /Get_LUN
ReadF, unit, strs
Free_LUN, unit
strList = List(strs, /EXTRACT)
--
Cheers,
-Dick
Dick Jackson Software Consulting Inc.
Victoria, BC, Canada
www.d-jackson.com
Paul van Delst wrote, On 2014-02-24, 9:20am:
> I created a test file, "test_string.txt", that contains the following (minus the
> "%<" lines):
>
> -----%<-----
> this is string one
> and now string two
> this little piggy cried wee wee wee...
> the end
> -----%<-----
>
> Then the procedure:
>
> -----%<-----
> pro read_into_list, user_list
> user_list = list()
> openr, fid, "test_string.txt", /get_lun
> cbuffer = ""
> while (~ eof(fid)) do begin
> readf, fid, cbuffer
> user_list.add, cbuffer
> endwhile
> free_lun, fid
> end
> -----%<-----
>
> Running the above in IDL:
>
>
> IDL> .run read_into_list
> % Compiled module: READ_INTO_LIST.
> IDL> read_into_list, x
> IDL> help, x
> X LIST <ID=1 NELEMENTS=4>
> IDL> for i=0, n_elements(x)-1 do help, x[i]
> <Expression> STRING = 'this is string one'
> <Expression> STRING = 'and now string two'
> <Expression> STRING = 'this little piggy cried wee wee wee...'
> <Expression> STRING = 'the end'
>
> When I edit the file and run again I get
>
> IDL> read_into_list, x
> IDL> for i=0, n_elements(x)-1 do help, x[i]
> <Expression> STRING = 'this is string one'
> <Expression> STRING = 'and now string two'
> <Expression> STRING = 'toss in a third string,'
> <Expression> STRING = 'this little piggy cried wee wee wee...'
> <Expression> STRING = 'the end'
> <Expression> STRING = 'not really. This the the last line'
>
>
> How's that?
>
>
> cheers,
>
> paulv
>
> On 02/23/14 17:31, johnson.dustin.astro@gmail.com wrote:
>> Hi,
>>
>>
>> I am writing a function/routine that makes use of a user provided
>> list
> of strings. Right now, the user needs to edit the LIST(a,b,c,d)
> parameter. I was wondering if there is a way for me to have the routine
> reference a .txt file and use the contents to populate the list, rather
> than have the user edit the function.
>>
>> Does anyone know how I would accomplish this?
>>
>> Thanks!
>>
|
|
|
Re: Using a text list to populate a LIST() in IDL [message #87715 is a reply to message #87711] |
Tue, 25 February 2014 06:09   |
Paul Van Delst[1]
Messages: 1157 Registered: April 2002
|
Senior Member |
|
|
Ooo... nice one. Much simpler/better.
On 02/24/14 23:04, Dick Jackson wrote:
> Hi folks,
>
> If an extra copy of the strings will fit in memory, this might be easier:
>
> f = 'test_string.txt'
> strs = StrArr(File_Lines(f))
> OpenR, unit, f, /Get_LUN
> ReadF, unit, strs
> Free_LUN, unit
> strList = List(strs, /EXTRACT)
>
|
|
|
Re: Using a text list to populate a LIST() in IDL [message #87747 is a reply to message #87693] |
Tue, 25 February 2014 20:06  |
johnson.dustin.astro
Messages: 2 Registered: February 2014
|
Junior Member |
|
|
On Sunday, February 23, 2014 2:31:14 PM UTC-8, johnson.du...@gmail.com wrote:
> Hi,
>
>
>
>
>
> I am writing a function/routine that makes use of a user provided list of strings. Right now, the user needs to edit the LIST(a,b,c,d) parameter. I was wondering if there is a way for me to have the routine reference a .txt file and use the contents to populate the list, rather than have the user edit the function.
>
>
>
> Does anyone know how I would accomplish this?
>
>
>
> Thanks!
Thank you both for your help, worked great!
|
|
|