Re: weird behaviour with cgdemodata [message #76611] |
Tue, 21 June 2011 10:53 |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
Jeremy Bailin writes:
> I'm sure there's a good reason why this happens, but it has me very confused:
>
> IDL> i = 5
> IDL> print, i, n_elements(cgdemodata(i))
> 4 65536
>
> Okay, that looks fine. Now what if I put that in a for loop?
>
>
> IDL> for i=0,5 do print, i, n_elements(cgdemodata(i))
> 0 101
> 0 101
> 0 101
> 0 101
> 0 101
> 0 101
> 0 101
> 0 101
> 0 101
> 0 101
> 0 101
> 0 101
> 0 101
> 0 101
> 0 101
> 0 101
> ...repeat ad infinutm, i.e. until you hit Ctrl-C
>
>
> What is going on???
This is one of those "pass by reference" things that
drives you crazy every once in awhile. It turns out
I am actually *changing* the value of the variable
number in the code. (It never occurred to me someone
would call this function in a loop, for God's sake!!)
If you pass the value "i" by value, you should get
what you expect:
IDL> for i=0,5 do print, i, n_elements(cgdemodata(i[0]))
0 101
1 101
2 1681
3 1681
4 65536
5 65536
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Sepore ma de ni thue. ("Perhaps thos speakest truth.")
|
|
|
|
Re: weird behaviour with cgdemodata [message #76616 is a reply to message #76614] |
Tue, 21 June 2011 09:10  |
Foldy Lajos
Messages: 268 Registered: October 2001
|
Senior Member |
|
|
On Tue, 21 Jun 2011, Jeremy Bailin wrote:
> That should of course be print, i not print, j....
>
That's why I prefer something like this (no extra variable):
for i=0,5 do print, i, n_elements(cgdemodata(i[0]))
regards,
Lajos
|
|
|
|
Re: weird behaviour with cgdemodata [message #76618 is a reply to message #76617] |
Tue, 21 June 2011 08:28  |
Jeremy Bailin
Messages: 618 Registered: April 2008
|
Senior Member |
|
|
Aha! Yes, that definitely explains it. I was questioning my sanity there for a minute. The following code, then, does what I was trying to do (which was basically to find random data sets that are the same size so Icould plot them against each other in some example code):
for i=0,25 do begin j=i & print, j, n_elements(cgdemodata(j)) & end
Thanks, Wayne for sleuthing this out...
-Jeremy.
|
|
|
Re: weird behaviour with cgdemodata [message #76619 is a reply to message #76618] |
Tue, 21 June 2011 08:27  |
ben.bighair
Messages: 221 Registered: April 2007
|
Senior Member |
|
|
Hi,
On 6/21/11 10:26 AM, Jeremy Bailin wrote:
> I'm sure there's a good reason why this happens, but it has me very confused:
>
> IDL> i = 5
> IDL> print, i, n_elements(cgdemodata(i))
> 4 65536
>
> Okay, that looks fine. Now what if I put that in a for loop?
>
>
> IDL> for i=0,5 do print, i, n_elements(cgdemodata(i))
> 0 101
> 0 101
> 0 101
> 0 101
> 0 101
> 0 101
> 0 101
> 0 101
> 0 101
> 0 101
> 0 101
> 0 101
> 0 101
> 0 101
> 0 101
> 0 101
> ...repeat ad infinutm, i.e. until you hit Ctrl-C
>
>
> What is going on???
>
> -Jeremy.
It looks like David overwrites the positional argument "number" in his
code - which you pass in as "i". "i" keeps getting reset to 0 in the
loop. It's the old pass-by-reference thing. I have posted a fix here...
http://dl.dropbox.com/u/8433654/cgdemodata.pro
but I'll send it along to David so he can decide what to do for the long
run.
Cheers,
Ben
|
|
|
Re: weird behaviour with cgdemodata [message #76620 is a reply to message #76619] |
Tue, 21 June 2011 08:19  |
wlandsman
Messages: 743 Registered: June 2000
|
Senior Member |
|
|
The problem is that the "input" parameter to cgdemodata is really an input-output parameter -- it is modified by cgdemodata(). For example,
IDL> i = 1 & print,N_elements(cgdemodata(i)) & print,i
101
0
The offending code in cgdemodata() is below. It should probably be rewritten so that the "number" parameter is left unmodified
FUNCTION cgDemoData_ReadData, number
IF N_Params() EQ 1 THEN BEGIN
type = Size(number)
type = type( type(0) + 1 )
IF type EQ 0 THEN Message, 'Supplied argument is undefined.'
IF type GT 5 THEN Message, 'Supplied argument must be a number.'
number = number - 1
number = 0 > number < 24
data = cgDemoData_ReadData(number)
RETURN, data
ENDIF
|
|
|