name a variable with number [message #87449] |
Tue, 04 February 2014 02:35  |
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 #87452 is a reply to message #87449] |
Tue, 04 February 2014 05:31   |
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   |
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   |
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 #87458 is a reply to message #87449] |
Tue, 04 February 2014 06:41   |
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   |
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   |
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 #87465 is a reply to message #87464] |
Tue, 04 February 2014 11:51  |
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.")
|
|
|