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

Home » Public Forums » archive » name a variable with number
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
name a variable with number [message #87449] Tue, 04 February 2014 02:35 Go to next message
gunvicsin11 is currently offline  gunvicsin11
Messages: 93
Registered: November 2012
Member
Hello everyone,
How to name a variable with number, for example,
if nn=dblarr(800)
I have to save it in this way n1=nn
similarly upto n25
Please let me know if you can help me out in this regard
thanking you in advance
sid
Re: name a variable with number [message #87450 is a reply to message #87449] Tue, 04 February 2014 03:59 Go to previous messageGo to next message
Mats Löfdahl is currently offline  Mats Löfdahl
Messages: 263
Registered: January 2012
Senior Member
Den tisdagen den 4:e februari 2014 kl. 11:35:27 UTC+1 skrev sid:
> Hello everyone,
> How to name a variable with number, for example,
> if nn=dblarr(800)
> I have to save it in this way n1=nn
> similarly upto n25
> Please let me know if you can help me out in this regard
> thanking you in advance
> sid

Not sure what you are asking, exactly. Naming or saving? But it seems to me that 2-dimensional arrays could somehow be useful. Like, nnn=dblarr(800,25).
Re: name a variable with number [message #87451 is a reply to message #87449] Tue, 04 February 2014 04:16 Go to previous messageGo to next message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
sid writes:

>
> Hello everyone,
> How to name a variable with number, for example,
> if nn=dblarr(800)
> I have to save it in this way n1=nn
> similarly upto n25
> Please let me know if you can help me out in this regard

I'm not sure what you asking to do either, but I'm pretty sure this is
NOT what you want to do. Can you explain what you want to do a bit more
clearly, and tell us why you want to do it?

Cheers,

David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
Sepore ma de ni thue. ("Perhaps thou speakest truth.")
Re: name a variable with number [message #87452 is a reply to message #87449] Tue, 04 February 2014 05:31 Go to previous messageGo to next message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
sid writes:

> The main problem is, I will have a resultant array of
> n=dblarr(i,j)
> j=25,
> but i is varying say around 800 to 900.
> so i should name it in a for loop like,
> n1=n(800,1)
> n2=n(830,2)
> n3=n(840,3)
> .
> .
> .
> like this it has to go till 25.
> Finally I have to save this n1,n2,n3,....,n25 into separate files,
> like
> openw,2,'test.dat'
> printf,2,n1
> close,2
> till n25
> So I have to find a way to save in a variable which varies like n1,n2,...,n25

You want to use a list, rather than an array. The list will allow you to
store vectors of various lengths, and you can then treat the list
variable as if it were an array when saving the data, etc.

Cheers,

David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
Sepore ma de ni thue. ("Perhaps thou speakest truth.")
Re: name a variable with number [message #87453 is a reply to message #87449] Tue, 04 February 2014 05:50 Go to previous messageGo to next message
rjp23 is currently offline  rjp23
Messages: 97
Registered: June 2010
Member
On Tuesday, February 4, 2014 10:35:27 AM UTC, sid wrote:
> Hello everyone,
>
> How to name a variable with number, for example,
>
> if nn=dblarr(800)
>
> I have to save it in this way n1=nn
>
> similarly upto n25
>
> Please let me know if you can help me out in this regard
>
> thanking you in advance
>
> sid


This is probably a "bad" way of doing it but I often use "execute" to generate variable names dynamically based on the contents of other variables which I think sounds like what you want to do.

For example:

for j=0, 24 do blah=execute('n'+strtrim(j,2)+'=nn[*,j]')

This will create the 25 arrays named n0, n1, n2, etc that contain nn[0,*], etc.

I'd be interested in what David et al. this of this as I end up using it quite often (but in more complicated situations).
Re: name a variable with number [message #87454 is a reply to message #87453] Tue, 04 February 2014 05:57 Go to previous messageGo to next message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
rjp23@le.ac.uk writes:

> This is probably a "bad" way of doing it but I often use "execute" to generate variable names dynamically based on the contents of other variables which I think sounds like what you want to do.
>
> For example:
>
> for j=0, 24 do blah=execute('n'+strtrim(j,2)+'=nn[*,j]')
>
> This will create the 25 arrays named n0, n1, n2, etc that contain nn[0,*], etc.
>
> I'd be interested in what David et al. this of this as I end up using it quite often (but in more complicated situations).

I think in the hands of someone who knows what they are doing this can
be a powerful programming technique. I rarely see it used in those kind
of hands. :-)

Cheers,

David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
Sepore ma de ni thue. ("Perhaps thou speakest truth.")
Re: name a variable with number [message #87456 is a reply to message #87454] Tue, 04 February 2014 06:09 Go to previous messageGo to next message
Fabzi is currently offline  Fabzi
Messages: 305
Registered: July 2010
Senior Member
Hi,


On 04.02.2014 14:57, David Fanning wrote:
> I think in the hands of someone who knows what they are doing this can
> be a powerful programming technique. I rarely see it used in those kind
> of hands.:-)

what David means is that most experienced programmers probably never
even *needed* to create a variable dynamically.

If the technique is available, this does not mean this technique is
suitable for your problem. If you think you need X variables named
var_0, var_1, ---, var_X than the algorithm you are thinking of is
probably not the best one.

In this case, a list (for IDL8+) or a pointer array (all versions) is
probably a better solution.

For example:

var = PTRARR(25)
for j=0, 24 do var[j]=PTR_NEW(whatever_you_want_to_put_inside)

Fab
Re: name a variable with number [message #87457 is a reply to message #87456] Tue, 04 February 2014 06:29 Go to previous messageGo to next message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
Fabien writes:

> On 04.02.2014 14:57, David Fanning wrote:
>> I think in the hands of someone who knows what they are doing this can
>> be a powerful programming technique. I rarely see it used in those kind
>> of hands.:-)
>
> what David means is that most experienced programmers probably never
> even *needed* to create a variable dynamically.

Well, that's partly what I meant. People who know what they are doing
don't generally find they need to use this technique. I could certainly
count on one or two fingers the times I've found it necessary. But, I
deliberately left my meaning nebulous to include a more Coyote-like
meaning, too. ;-)

Cheers,

David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
Sepore ma de ni thue. ("Perhaps thou speakest truth.")
Re: name a variable with number [message #87458 is a reply to message #87449] Tue, 04 February 2014 06:41 Go to previous messageGo to next message
Mats Löfdahl is currently offline  Mats Löfdahl
Messages: 263
Registered: January 2012
Senior Member
Den tisdagen den 4:e februari 2014 kl. 14:27:53 UTC+1 skrev sid:
> Hello sir,
>
> The main problem is, I will have a resultant array of
> n=dblarr(i,j)
> j=25,
> but i is varying say around 800 to 900.
> so i should name it in a for loop like,
>
> n1=n(800,1)
> n2=n(830,2)
> n3=n(840,3)
> .
> .
> .
> like this it has to go till 25.

I assume you mean n1=n(0:799,0), etc?

But why do you have to make each column in the 2D array into a separate variable? Isn't the data fine where it is?

> Finally I have to save this n1,n2,n3,....,n25 into separate files,
> like
>
> openw,2,'test.dat'
> printf,2,n1
> close,2
>
> till n25

This should work just as well with the original 2D array. Assume you have the lengths of the columns in an array lengths=[800,830,840,...], you could do something like:

for i=0,24 do begin
openw,2,'test'+strtrim(i,2)+'.dat'
printf,2,n[0:lengths[i]-1,i]
close,2
end
Re: name a variable with number [message #87459 is a reply to message #87456] Tue, 04 February 2014 09:28 Go to previous messageGo to next message
rjp23 is currently offline  rjp23
Messages: 97
Registered: June 2010
Member
On Tuesday, February 4, 2014 2:09:16 PM UTC, Fabien wrote:
> Hi,
>
>
>
>
>
> On 04.02.2014 14:57, David Fanning wrote:
>
>> I think in the hands of someone who knows what they are doing this can
>
>> be a powerful programming technique. I rarely see it used in those kind
>
>> of hands.:-)
>
>
>
> what David means is that most experienced programmers probably never
>
> even *needed* to create a variable dynamically.
>
>
>
> If the technique is available, this does not mean this technique is
>
> suitable for your problem. If you think you need X variables named
>
> var_0, var_1, ---, var_X than the algorithm you are thinking of is
>
> probably not the best one.
>
>
>
> In this case, a list (for IDL8+) or a pointer array (all versions) is
>
> probably a better solution.
>
>
>
> For example:
>
>
>
> var = PTRARR(25)
>
> for j=0, 24 do var[j]=PTR_NEW(whatever_you_want_to_put_inside)
>
>
>
> Fab


If anyone feels the inclination to cover this in more detail it'd be appreciated as I find myself using it quite often, normally as a shortcut to repeating the same thing over and over.

For example, say I have many different models datasets where I want to interact on the data on each individual model but I also want all the datasets to be available. I typically name the data modelA_variableA, modelB_variableA, etc and then loop over by setting thisModel_variableA=modelA_variableA using the type of execute command I posted above with "modelA" and "variableA" as the strings.

Is there a better (generic) way of doing this?
Re: name a variable with number [message #87460 is a reply to message #87459] Tue, 04 February 2014 10:00 Go to previous messageGo to next message
Fabzi is currently offline  Fabzi
Messages: 305
Registered: July 2010
Senior Member
On 04.02.2014 18:28, rjp23@le.ac.uk wrote:
> Is there a better (generic) way of doing this?

I am not an expert enough, but if you are using a loop to generate
variables dynamically, and then using a loop again somewhere else to
read them, then you use the variables as "data holder" and this "data
holder" could be anything else. You have created a variable
modelB_variableA, what do you do with it afterwards? How do you check
later in your code that ModelX_VariableY is available to continue
processing?
This is much easier to test if you have a variable, say, ModelVar as a
pointer array and check if ModelVar[X,Y] is a valid pointer, for example.

For the problem you describe, HASH() in IDL 8+ could quite adapted. For
earlier versions, I guess a solution based on structures and/or pointers
could work.

There is nothing "wrong" in generating variables dynamically, it just
makes code difficult to understand and to debug. If you have something
that works, it's probably not necessary to change your code now, I guess...
Re: name a variable with number [message #87461 is a reply to message #87459] Tue, 04 February 2014 10:07 Go to previous messageGo to next message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
rjp23@le.ac.uk writes:

> If anyone feels the inclination to cover this in more detail it'd be appreciated as I find myself using it quite often, normally as a shortcut to repeating the same thing over and over.

I think we are all confused about how hard-coding more variable names
can be a shortcut to doing *anything* over and over again. It seems to
me to be the *last* thing you would want to do. Doing things over and
over again are exactly what arrays and lists are good for.

Giving things names may be good for the soul (In the beginning was the
Word, and the Word was with God, etc.), but doing it in a program just
seems crazy. :-)

Cheers,

David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
Sepore ma de ni thue. ("Perhaps thou speakest truth.")
Re: name a variable with number [message #87464 is a reply to message #87461] Tue, 04 February 2014 11:45 Go to previous messageGo to next message
rjp23 is currently offline  rjp23
Messages: 97
Registered: June 2010
Member
On Tuesday, February 4, 2014 6:07:16 PM UTC, David Fanning wrote:
>
> I think we are all confused about how hard-coding more variable names
>
> can be a shortcut to doing *anything* over and over again. It seems to
>
> me to be the *last* thing you would want to do. Doing things over and
>
> over again are exactly what arrays and lists are good for.
>

How I use it is slightly different to the OP, maybe hence the confusion. I don't so much use it to split up data but to treat data from various sources the same way. It's pretty much the opposite of having hard-coded variable names.

e.g. It's normally when I have data in the same format from various different sources (usually different model runs) which I want to do the same thing to. Rather than have a chunk of code for each data source, to do the same thing to modelA, modelB, modelC, etc I just use execute to make thisModel=modelA within a loop and then act on each in turn.

That probably doesn't make any more sense though ;-)
Re: name a variable with number [message #87465 is a reply to message #87464] Tue, 04 February 2014 11:51 Go to previous message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
rjp23@le.ac.uk writes:

> How I use it is slightly different to the OP, maybe hence the confusion. I don't so much use it to split up data but to treat data from various sources the same way. It's pretty much the opposite of having hard-coded variable names.
>
> e.g. It's normally when I have data in the same format from various different sources (usually different model runs) which I want to do the same thing to. Rather than have a chunk of code for each data source, to do the same thing to modelA, modelB, modelC, etc I just use execute to make thisModel=modelA within a loop and then act on each in turn.
>
> That probably doesn't make any more sense though ;-)

Well, not to me, it doesn't. ;-)

In a loop like this, when I want clarity, I usually do something like
this. Is this what you mean?

FOR j=0,n DO BEGIN
thisModel = theModels[j]
DoSomethingWith, thisModel
....
ENDFOR

Cheers,

David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
Sepore ma de ni thue. ("Perhaps thou speakest truth.")
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: accessing IDL internal (aka "built-in") routines
Next Topic: Error using data structures while reading ASCII file with different data types

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

Current Time: Wed Oct 08 15:49:43 PDT 2025

Total time taken to generate the page: 0.00749 seconds