Re: Name of arrays [message #79257] |
Mon, 20 February 2012 07:01  |
jeffnettles4870
Messages: 111 Registered: October 2006
|
Senior Member |
|
|
On Feb 20, 4:46 am, Israel Rodriguez Hermelo <israelherm...@gmail.com>
wrote:
> On Feb 19, 5:00 pm, David Fanning <n...@idlcoyote.com> wrote:
>
>
>
>
>
>
>
>
>
>> Israel Rodriguez Hermelo writes:
>>> Why did you say that's a slippery slope? Is there a better solution?
>>> I'm already using it with no problems.
>
>> I see a lot of people who are just getting started with
>> programming wanting to name their variables like this.
>> It seems cool, but in the end it just leads to unmaintainable
>> programs. A variable is a variable. The variable named "a" is
>> just as good as the variable named "b". Yes, variables should
>> have good names in programs. But just give them a name
>> (e.g., "theseMonths") in the program module where you need them.
>> Don't go looking for them all over God's creation!
>
>> Cheers,
>
>> David
>
>> --
>> David Fanning, Ph.D.
>> Fanning Software Consulting, Inc.
>> Coyote's Guide to IDL Programming:http://www.idlcoyote.com/
>> Sepore ma de ni thui. ("Perhaps thou speakest truth.")
>
> Thank you for your advice David. I'll try to avoid using
> scope_varfecht then.
> My problem however is not simply that I want to have pretty names for
> my variables. The problem is that I don't know a priori the number of
> arrays that I will need nor their size.
> For example, in some cases the input data might correspond to the
> months of February and July, but in other cases might correspond to
> January, March and April.
>
> MONTH = [ 'FEBRUARY' , 'JULY' , 'SEPTEMBER' ]
>
> To deal with this, I was trying:
>
> Nmonths=N_ELEMENTS(MONTH)
>
> for i=0, Nmonths-1 do begin
>
> readcol, MONTH(i)+'_data.txt', TEMPERATURE
>
> Ndays=N_ELEMENTS(TEMPERATURE)
>
> (scope_varfetch(MONTH(r)+'TEMPERATURE', /enter))=fltarr(Ndays)
>
> endfor
>
> I see why you wrote that scope_varfecht makes the leads to
> unmaintainable
> programs and I would prefer any other solution but I've been looking
> for it in the forum and I haven't found any. Do you have any
> suggestion?
> Thanks in advance!
>
> Regards,
>
> Israel
Could you just use 31 element arrays for all months and just have some
empty elements at the end of some of those arrays? It depends on what
you're going to do with the arrays of course, but this is the first
thing i'd try!
Jeff
|
|
|
|
Re: Name of arrays [message #79260 is a reply to message #79259] |
Mon, 20 February 2012 01:46   |
Israel Rodriguez Herm
Messages: 13 Registered: February 2012
|
Junior Member |
|
|
On Feb 19, 5:00 pm, David Fanning <n...@idlcoyote.com> wrote:
> Israel Rodriguez Hermelo writes:
>> Why did you say that's a slippery slope? Is there a better solution?
>> I'm already using it with no problems.
>
> I see a lot of people who are just getting started with
> programming wanting to name their variables like this.
> It seems cool, but in the end it just leads to unmaintainable
> programs. A variable is a variable. The variable named "a" is
> just as good as the variable named "b". Yes, variables should
> have good names in programs. But just give them a name
> (e.g., "theseMonths") in the program module where you need them.
> Don't go looking for them all over God's creation!
>
> Cheers,
>
> David
>
> --
> David Fanning, Ph.D.
> Fanning Software Consulting, Inc.
> Coyote's Guide to IDL Programming:http://www.idlcoyote.com/
> Sepore ma de ni thui. ("Perhaps thou speakest truth.")
Thank you for your advice David. I'll try to avoid using
scope_varfecht then.
My problem however is not simply that I want to have pretty names for
my variables. The problem is that I don't know a priori the number of
arrays that I will need nor their size.
For example, in some cases the input data might correspond to the
months of February and July, but in other cases might correspond to
January, March and April.
MONTH = [ 'FEBRUARY' , 'JULY' , 'SEPTEMBER' ]
To deal with this, I was trying:
Nmonths=N_ELEMENTS(MONTH)
for i=0, Nmonths-1 do begin
readcol, MONTH(i)+'_data.txt', TEMPERATURE
Ndays=N_ELEMENTS(TEMPERATURE)
(scope_varfetch(MONTH(r)+'TEMPERATURE', /enter))=fltarr(Ndays)
endfor
I see why you wrote that scope_varfecht makes the leads to
unmaintainable
programs and I would prefer any other solution but I've been looking
for it in the forum and I haven't found any. Do you have any
suggestion?
Thanks in advance!
Regards,
Israel
|
|
|
|
Re: Name of arrays [message #79265 is a reply to message #79264] |
Sun, 19 February 2012 07:06   |
Israel Rodriguez Herm
Messages: 13 Registered: February 2012
|
Junior Member |
|
|
On Feb 19, 4:03 pm, David Fanning <n...@idlcoyote.com> wrote:
> Lajos Foldy writes:
>
>> On Feb 19, 1:30 pm, Israel Rodriguez Hermelo <israelherm...@gmail.com>
>> wrote:
>>> Hi there!
>
>>> I'd like to use a string as part of the name of an array:
>
>>> MONTH = [ 'FEBRUARY' , 'JULY' ]
>
>>> TEMPERATURE+MONTH(0)=fltarr(28)
>
>>> TEMPERATURE+MONTH(1)=fltarr(31)
>
>>> I found any way to do this simple task. Anybody can help me?
>
>>> Thanks in advance!
>
>> (scope_varfetch('TEMPERATURE'+MONTH(0), /enter))=fltarr(28)
>
> Be careful. That's a slippery slope and rarely a
> good idea. :-)
>
> Cheers,
>
> David
>
> --
> David Fanning, Ph.D.
> Fanning Software Consulting, Inc.
> Coyote's Guide to IDL Programming:http://www.idlcoyote.com/
> Sepore ma de ni thui. ("Perhaps thou speakest truth.")
Why did you say that's a slippery slope? Is there a better solution?
I'm already using it with no problems.
Regards,
Israel
|
|
|
|
|
|
Re: Name of arrays [message #79334 is a reply to message #79260] |
Tue, 21 February 2012 03:53  |
greg.addr
Messages: 160 Registered: May 2007
|
Senior Member |
|
|
Well, one more suggestion - forget the months: keep the data indexed by day. Then you can have one month, three or 13 without a problem, and - probably most usefully - you can even plot them all at once! No need for scope_varfetch, lists, pointers, structures, 28-day month fillers or any of that clever stuff...
IDL has plenty of functions to convert day indices back to dates if you want to have them in your output:
IDL> print,julday(1,1,2011)
2455563
IDL> caldat,2455563,m,d,y
IDL> print,m,d,y
1 1 2011
IDL> caldat,2455563+1,m,d,y
IDL> print,m,d,y
1 2 2011
IDL> print,2455563,format='(C(CMoA, X, CDI2.2, X, CYI5))'
Jan 01 2011
Greg
|
|
|
Re: Name of arrays [message #79346 is a reply to message #79260] |
Mon, 20 February 2012 13:27  |
Russell[1]
Messages: 101 Registered: August 2011
|
Senior Member |
|
|
On Feb 20, 4:46 am, Israel Rodriguez Hermelo <israelherm...@gmail.com>
wrote:
> On Feb 19, 5:00 pm, David Fanning <n...@idlcoyote.com> wrote:
>
>
>
>
>
>
>
>
>
>> Israel Rodriguez Hermelo writes:
>>> Why did you say that's a slippery slope? Is there a better solution?
>>> I'm already using it with no problems.
>
>> I see a lot of people who are just getting started with
>> programming wanting to name their variables like this.
>> It seems cool, but in the end it just leads to unmaintainable
>> programs. A variable is a variable. The variable named "a" is
>> just as good as the variable named "b". Yes, variables should
>> have good names in programs. But just give them a name
>> (e.g., "theseMonths") in the program module where you need them.
>> Don't go looking for them all over God's creation!
>
>> Cheers,
>
>> David
>
>> --
>> David Fanning, Ph.D.
>> Fanning Software Consulting, Inc.
>> Coyote's Guide to IDL Programming:http://www.idlcoyote.com/
>> Sepore ma de ni thui. ("Perhaps thou speakest truth.")
>
> Thank you for your advice David. I'll try to avoid using
> scope_varfecht then.
> My problem however is not simply that I want to have pretty names for
> my variables. The problem is that I don't know a priori the number of
> arrays that I will need nor their size.
> For example, in some cases the input data might correspond to the
> months of February and July, but in other cases might correspond to
> January, March and April.
>
> MONTH = [ 'FEBRUARY' , 'JULY' , 'SEPTEMBER' ]
>
> To deal with this, I was trying:
>
> Nmonths=N_ELEMENTS(MONTH)
>
> for i=0, Nmonths-1 do begin
>
> readcol, MONTH(i)+'_data.txt', TEMPERATURE
>
> Ndays=N_ELEMENTS(TEMPERATURE)
>
> (scope_varfetch(MONTH(r)+'TEMPERATURE', /enter))=fltarr(Ndays)
>
> endfor
>
> I see why you wrote that scope_varfecht makes the leads to
> unmaintainable
> programs and I would prefer any other solution but I've been looking
> for it in the forum and I haven't found any. Do you have any
> suggestion?
> Thanks in advance!
>
> Regards,
>
> Israel
This is where you should use the pointers. Consider the following:
months=['Jan','Feb','Sep']
x=ptr_new(months)
help,(*x)
But, months can change on-the-fly and contain any data type
data=[{month:'Jan',numberofdays:31},{month:'Feb',numberofday s:28}]
x=ptr_new(data)
help,(*x),/str
and so on. Since you can do any operation on (*x) that you would do
on say months=['Jan','Feb','Sep'], this usage means you can
arbitrarily define the variables and access the data. Now, it's true
that as of IDL 8.* they have introduced new variable types to do this,
but until IDL 8 is standard everywhere, you might consider remaining
with pointers (for backwards compatibility).
Russell
|
|
|
|