putting .txt files in a structure [message #91679] |
Wed, 12 August 2015 13:50  |
wdolan
Messages: 29 Registered: June 2015
|
Junior Member |
|
|
So I've created a structure which has a bunch of arrays (both string, and integer). I handwrote a file called readme.txt which explains all the variables in the structure. I can't get the file itself to become part of the structure.
Part of the code looks like this (not the actual code, but similar..):
btstruc={mm=momentmm,$
ros=btstruc.ros, $
readme=readme.txt}
Everything works, except the text file!
Any ideas?
|
|
|
|
Re: putting .txt files in a structure [message #91681 is a reply to message #91680] |
Wed, 12 August 2015 14:23   |
wdolan
Messages: 29 Registered: June 2015
|
Junior Member |
|
|
Hi Paul.
You are correct in assuming that. I would like the text file as a whole to be part of the structure, so that when I hand off my structure to another person, they can know what all the variables are.
On Wednesday, August 12, 2015 at 2:05:06 PM UTC-7, Paul van Delst wrote:
> On 08/12/15 16:50, Wayana Dolan wrote:
>> So I've created a structure which has a bunch of arrays (both
>> string,
>> and integer). I handwrote a file called readme.txt which explains all
>> the variables in the structure. I can't get the file itself to become
>> part of the structure.
>
> I'm not sure exactly what you mean by "get the file itself to become
> part of the structure."
>
> Do you mean the file *name*? [easy]
>
> Or...
>
> Do you mean the file *contents*? [hard]
>
>> Part of the code looks like this (not the actual code, but similar..):
>> btstruc={mm=momentmm,$
>> ros=btstruc.ros, $
>> readme=readme.txt}
>>
>> Everything works, except the text file!
>
> The above pseudo-code tells me you are trying to get the file *name* as
> part of the structure.
>
> But somehow I don't think that's what you want....right?
|
|
|
Re: putting .txt files in a structure [message #91682 is a reply to message #91681] |
Wed, 12 August 2015 14:52   |
Paul Van Delst[1]
Messages: 1157 Registered: April 2002
|
Senior Member |
|
|
On 08/12/15 17:23, Wayana Dolan wrote:
> Hi Paul.
>
> You are correct in assuming that. I would like the text file as a
> whole to be part of the structure, so that when I hand off my structure
> to another person, they can know what all the variables are.
Ah. Well, then, you need to write code to read said text file, create
the (or a) structure based on what you read (e.g. tag names), and then
populate your newly created structure with values from the file.
It's a fair bit of work (in any language).
I would look at create_struct. It's what I use to read netcdf files into
a structure. Same deal as what you're doing apart from teh file format.
Other folks may have better/simpler ideas based on their experience.
E.g. is there a "read_*" function/procedure to do it?
cheers,
paulv
>
>
> On Wednesday, August 12, 2015 at 2:05:06 PM UTC-7, Paul van Delst wrote:
>> On 08/12/15 16:50, Wayana Dolan wrote:
>>> So I've created a structure which has a bunch of arrays (both
>>> string,
>>> and integer). I handwrote a file called readme.txt which explains all
>>> the variables in the structure. I can't get the file itself to become
>>> part of the structure.
>>
>> I'm not sure exactly what you mean by "get the file itself to become
>> part of the structure."
>>
>> Do you mean the file *name*? [easy]
>>
>> Or...
>>
>> Do you mean the file *contents*? [hard]
>>
>>> Part of the code looks like this (not the actual code, but similar..):
>>> btstruc={mm=momentmm,$
>>> ros=btstruc.ros, $
>>> readme=readme.txt}
>>>
>>> Everything works, except the text file!
>>
>> The above pseudo-code tells me you are trying to get the file *name* as
>> part of the structure.
>>
>> But somehow I don't think that's what you want....right?
>
|
|
|
Re: putting .txt files in a structure [message #91685 is a reply to message #91682] |
Wed, 12 August 2015 22:57   |
Michael Galloy
Messages: 1114 Registered: April 2006
|
Senior Member |
|
|
Do you mean you want to have the full text of the file as a string in
your structure?
If so, it's not so bad:
nlines = file_lines(filename)
lines = strarr(nlines)
openr, lun, filename, /get_lun
readf, lun, lines
free_lun, lun
cr = string([10B]) ; string([13B, 10B]) on Windows
text = strjoin(lines, cr)
If you have my library, it's just:
text = mg_strmerge(mg_file(filename, /readf))
Then:
btstruc = {mm: momentmm,$
ros: btstruc.ros, $
readme: text}
-Mike
On 8/12/15 3:52 PM, Paul van Delst wrote:
>
>
> On 08/12/15 17:23, Wayana Dolan wrote:
>> Hi Paul.
>>
>> You are correct in assuming that. I would like the text file as a
>> whole to be part of the structure, so that when I hand off my structure
>> to another person, they can know what all the variables are.
>
> Ah. Well, then, you need to write code to read said text file, create
> the (or a) structure based on what you read (e.g. tag names), and then
> populate your newly created structure with values from the file.
>
> It's a fair bit of work (in any language).
>
> I would look at create_struct. It's what I use to read netcdf files into
> a structure. Same deal as what you're doing apart from teh file format.
>
> Other folks may have better/simpler ideas based on their experience.
> E.g. is there a "read_*" function/procedure to do it?
>
> cheers,
>
> paulv
>
>>
>>
>> On Wednesday, August 12, 2015 at 2:05:06 PM UTC-7, Paul van Delst wrote:
>>> On 08/12/15 16:50, Wayana Dolan wrote:
>>>> So I've created a structure which has a bunch of arrays (both
>>>> string,
>>>> and integer). I handwrote a file called readme.txt which explains all
>>>> the variables in the structure. I can't get the file itself to become
>>>> part of the structure.
>>>
>>> I'm not sure exactly what you mean by "get the file itself to become
>>> part of the structure."
>>>
>>> Do you mean the file *name*? [easy]
>>>
>>> Or...
>>>
>>> Do you mean the file *contents*? [hard]
>>>
>>>> Part of the code looks like this (not the actual code, but similar..):
>>>> btstruc={mm=momentmm,$
>>>> ros=btstruc.ros, $
>>>> readme=readme.txt}
>>>>
>>>> Everything works, except the text file!
>>>
>>> The above pseudo-code tells me you are trying to get the file *name* as
>>> part of the structure.
>>>
>>> But somehow I don't think that's what you want....right?
>>
--
Michael Galloy
www.michaelgalloy.com
Modern IDL: A Guide to IDL Programming (http://modernidl.idldev.com)
|
|
|
Re: putting .txt files in a structure [message #91693 is a reply to message #91685] |
Thu, 13 August 2015 07:05  |
Paul Van Delst[1]
Messages: 1157 Registered: April 2002
|
Senior Member |
|
|
Hello,
On 08/13/15 01:57, Michael Galloy wrote:
> Do you mean you want to have the full text of the file as a string in
> your structure?
Oh. I though he wanted the file text parsed into tags:values. E.g. if
the file text was something like:
parameter1 3.14159
threshold2 2.71828
then the resulting structure (using IDL definition) would look something
like
text = {parameter1 : 3.14159, $
threshold2 : 2.71828}
and then, via something like:
text = read_myfile("readme.txt")
you could then do
btstruc = {mm: momentmm,$
ros: btstruc.ros, $
readme: text}
and then
IDL> help, btstruc.readme
** Structure <df05a8>, 2 tags, length=8, data length=8, refs=3:
PARAMETER1 FLOAT 3.14159
THRESHOLD2 FLOAT 2.71828
>
> If so, it's not so bad:
>
> nlines = file_lines(filename)
> lines = strarr(nlines)
> openr, lun, filename, /get_lun
> readf, lun, lines
> free_lun, lun
>
> cr = string([10B]) ; string([13B, 10B]) on Windows
> text = strjoin(lines, cr)
>
> If you have my library, it's just:
>
> text = mg_strmerge(mg_file(filename, /readf))
>
> Then:
>
> btstruc = {mm: momentmm,$
> ros: btstruc.ros, $
> readme: text}
But, yes, now I see this may be what he meant (and much simpler).
cheers,
paulv
p.s. Some days I always get the answer for "What colour is General
MacArthur's white horse?" wrong.
|
|
|