Re: Passing variables [message #55436] |
Thu, 23 August 2007 08:39  |
Conor
Messages: 138 Registered: February 2007
|
Senior Member |
|
|
On Aug 23, 11:33 am, jeffw...@hotmail.com wrote:
>> The easiest way is typically to use a common block. At the start of
>> everyone program that want to access some variables, put a line like
>> this:
>
>> common, block_name, variable1, variable2, ....
>
>> A couple rules about common blocks:
>
>> 1) The names of the variables don't have to be the same in every
>> program.
>> 2) The number of variables matter. All common blocks should have the
>> same number of variables.
>> 3) The positions of the variables matter. Since names don't matter,
>> the first variable in one program will always be the same as the first
>> variable in another program, even if they have much different names
>> 4) Common blocks last until you quit IDL. If you declare it once, it
>> stays declared.
>> 5) Common blocks can't be extended. This means that if you create a
>> common block and later realize you need to add more variables to it,
>> you'll have to quit IDL (or type .reset) in order to "destroy" the
>> common block.
>
> In the help files the only examples I see of common blocks are inside
> procedures. Can I put them inside a function, or even my main
> program? I tried putting:
>
> common, share, B
>
> in both my main program and the program with the function I'm using,
> and both came back as syntax errors.
>
> On second thought, after trying to answer my own program, I made a
> test program along the lines of:
>
> pro test
> common, share, A
>
> ...
>
> end
>
> IDL is insisting that the "common,share,A" is a syntax error.
Oops! Sorry, that's my mistake (I only use common blocks
occasionally). The exact syntax is:
common share,A
|
|
|
Re: Passing variables [message #55437 is a reply to message #55436] |
Thu, 23 August 2007 08:33   |
jeffwax1
Messages: 10 Registered: June 2004
|
Junior Member |
|
|
> The easiest way is typically to use a common block. At the start of
> everyone program that want to access some variables, put a line like
> this:
>
> common, block_name, variable1, variable2, ....
>
> A couple rules about common blocks:
>
> 1) The names of the variables don't have to be the same in every
> program.
> 2) The number of variables matter. All common blocks should have the
> same number of variables.
> 3) The positions of the variables matter. Since names don't matter,
> the first variable in one program will always be the same as the first
> variable in another program, even if they have much different names
> 4) Common blocks last until you quit IDL. If you declare it once, it
> stays declared.
> 5) Common blocks can't be extended. This means that if you create a
> common block and later realize you need to add more variables to it,
> you'll have to quit IDL (or type .reset) in order to "destroy" the
> common block.
In the help files the only examples I see of common blocks are inside
procedures. Can I put them inside a function, or even my main
program? I tried putting:
common, share, B
in both my main program and the program with the function I'm using,
and both came back as syntax errors.
On second thought, after trying to answer my own program, I made a
test program along the lines of:
pro test
common, share, A
...
end
IDL is insisting that the "common,share,A" is a syntax error.
|
|
|
|
Re: Passing variables [message #55442 is a reply to message #55441] |
Thu, 23 August 2007 06:34   |
Conor
Messages: 138 Registered: February 2007
|
Senior Member |
|
|
On Aug 22, 4:07 pm, jeffw...@hotmail.com wrote:
> I am having an issue passing variables from one program to another.
>
> I have a main program with a gigantic for loop. Each time through the
> loop there is a different value from a "B" array. Inside the loop I
> use rk4 to create fieldlines, with the derivatives ("dydx") calculated
> in another program. rk4 only allows you to give it a vector (say
> Xo,Yo) and it gives you back another vector (say X1,Y1). But I need,
> somehow to pass the value from my "B" array to the second program (the
> one that returns the "dydx").
>
> I tried putting a procedure in the middle of my main program along the
> lines of:
>
> pro getb,dummy
> return, [B(n)]
> end
>
> and then in the dydx program had a line with:
>
> myB = getb(0)
>
> But that didn't seem to work. Does anyone know an easy way to pass
> this B variable into this second program?
>
> Thanks.
The easiest way is typically to use a common block. At the start of
everyone program that want to access some variables, put a line like
this:
common, block_name, variable1, variable2, ....
A couple rules about common blocks:
1) The names of the variables don't have to be the same in every
program.
2) The number of variables matter. All common blocks should have the
same number of variables.
3) The positions of the variables matter. Since names don't matter,
the first variable in one program will always be the same as the first
variable in another program, even if they have much different names
4) Common blocks last until you quit IDL. If you declare it once, it
stays declared.
5) Common blocks can't be extended. This means that if you create a
common block and later realize you need to add more variables to it,
you'll have to quit IDL (or type .reset) in order to "destroy" the
common block.
For more on common blocks, see the online_help
Your getb program doesn't work for two reasons. First, you have a
bug. I suspect you meant to say:
pro getb,n
return, [B(n)]
end
or:
pro getb,dummy
return, [B(dummy)]
end
Also, it doesn't work because of variable scope. A sub-routine
automatically runs in it's own 'scope' which means that it can only
access variables it has been passed directly, it has created itself,
or that it retrieves from a common block. In the case above the 'B'
variable meets none of those criteria and is inaccessible to your getb
function. You could make this work anyway, using the scope_varfetch
function, but you are much better off using a common block, especially
since usage of scope_varfetch should be avoided.
|
|
|
Re: Passing variables [message #55532 is a reply to message #55437] |
Thu, 23 August 2007 08:59  |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
jeffwax1@hotmail.com writes:
> In the help files the only examples I see of common blocks are inside
> procedures. Can I put them inside a function, or even my main
> program?
You can put them wherever you like. A function in IDL
is just a procedure that returns a specific value. Other
than that, they are identical in functionality, including
how you pass data into and out of them, etc.
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
|
|
|
Re: Passing variables [message #55534 is a reply to message #55436] |
Thu, 23 August 2007 08:55  |
jeffwax1
Messages: 10 Registered: June 2004
|
Junior Member |
|
|
On Aug 23, 10:39 am, Conor <cmanc...@gmail.com> wrote:
> On Aug 23, 11:33 am, jeffw...@hotmail.com wrote:
>
>
>
>>> The easiest way is typically to use a common block. At the start of
>>> everyone program that want to access some variables, put a line like
>>> this:
>
>>> common, block_name, variable1, variable2, ....
>
>>> A couple rules about common blocks:
>
>>> 1) The names of the variables don't have to be the same in every
>>> program.
>>> 2) The number of variables matter. All common blocks should have the
>>> same number of variables.
>>> 3) The positions of the variables matter. Since names don't matter,
>>> the first variable in one program will always be the same as the first
>>> variable in another program, even if they have much different names
>>> 4) Common blocks last until you quit IDL. If you declare it once, it
>>> stays declared.
>>> 5) Common blocks can't be extended. This means that if you create a
>>> common block and later realize you need to add more variables to it,
>>> you'll have to quit IDL (or type .reset) in order to "destroy" the
>>> common block.
>
>> In the help files the only examples I see of common blocks are inside
>> procedures. Can I put them inside a function, or even my main
>> program? I tried putting:
>
>> common, share, B
>
>> in both my main program and the program with the function I'm using,
>> and both came back as syntax errors.
>
>> On second thought, after trying to answer my own program, I made a
>> test program along the lines of:
>
>> pro test
>> common, share, A
>
>> ...
>
>> end
>
>> IDL is insisting that the "common,share,A" is a syntax error.
>
> Oops! Sorry, that's my mistake (I only use common blocks
> occasionally). The exact syntax is:
>
> common share,A
Yeah, just figured out it was a mistake with excess commas. With the
fix it did work. Thanks.
|
|
|