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

Home » Public Forums » archive » Re: Name of arrays
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
Re: Name of arrays [message #79257] Mon, 20 February 2012 07:01 Go to next message
jeffnettles4870 is currently offline  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 #79259 is a reply to message #79257] Mon, 20 February 2012 03:01 Go to previous messageGo to next message
Fabzi is currently offline  Fabzi
Messages: 305
Registered: July 2010
Senior Member
Hi

On 02/20/2012 10:46 AM, Israel Rodriguez Hermelo wrote:
> The problem is that I don't know a priori the number of
> arrays that I will need nor their size.

If you have IDL 8.* you could use lists. Or you could use pointer arrays:

months = ['FEBRUARY' , 'JULY' , 'SEPTEMBER']
data = PTRARR(N_ELEMENTS(months))
data[0] = PTR_NEW(INDGEN(28))
temp_feb = *data[(where(months eq 'FEBRUARY'))[0]]
IDL> help, temp_feb
TEMP_FEB INT = Array[28]
Re: Name of arrays [message #79260 is a reply to message #79259] Mon, 20 February 2012 01:46 Go to previous messageGo to next message
Israel Rodriguez Herm is currently offline  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 #79264 is a reply to message #79260] Sun, 19 February 2012 08:00 Go to previous messageGo to next message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
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.")
Re: Name of arrays [message #79265 is a reply to message #79264] Sun, 19 February 2012 07:06 Go to previous messageGo to next message
Israel Rodriguez Herm is currently offline  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 #79266 is a reply to message #79265] Sun, 19 February 2012 07:03 Go to previous messageGo to next message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
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.")
Re: Name of arrays [message #79267 is a reply to message #79266] Sun, 19 February 2012 06:54 Go to previous messageGo to next message
Israel Rodriguez Herm is currently offline  Israel Rodriguez Herm
Messages: 13
Registered: February 2012
Junior Member
On Feb 19, 2:31 pm, Lajos Foldy <fawltylangu...@gmail.com> wrote:
> 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)
>
> regards,
> Lajos

Thank you very much!
Re: Name of arrays [message #79268 is a reply to message #79267] Sun, 19 February 2012 05:31 Go to previous messageGo to next message
Lajos Foldy is currently offline  Lajos Foldy
Messages: 176
Registered: December 2011
Senior Member
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)

regards,
Lajos
Re: Name of arrays [message #79334 is a reply to message #79260] Tue, 21 February 2012 03:53 Go to previous message
greg.addr is currently offline  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 Go to previous message
Russell[1] is currently offline  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
Re: Name of arrays [message #79352 is a reply to message #79259] Mon, 20 February 2012 10:15 Go to previous message
Brian Wolven is currently offline  Brian Wolven
Messages: 94
Registered: May 2011
Member
Or create_struct, using the month as the tag name, for pretty much any version of IDL.
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: tracking clusters through multiple timesteps
Next Topic: Issue with console of workbench 8.0

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

Current Time: Wed Oct 08 13:52:12 PDT 2025

Total time taken to generate the page: 0.00445 seconds