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

Home » Public Forums » archive » Re: Weird MIN behavior
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: Weird MIN behavior [message #42716] Mon, 21 February 2005 16:56
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
Michael Wallace writes:

> Ah, yes. I misspoke. A lot of the time I work with the arrays elements
> as pointers, so I just begin to think that everything is a pointer. But
> yes, if an array element is a scalar, it will be passed by value.

I know the feeling. I told someone the other day that
I thought I had completely forgotten how to write a
real widget program. Everything I touch these days are
objects. What a wonderful world to live in. :-)

Cheers,

David

P.S. Well, it *would* be wonderful if I could count on
"as advertised" consistencyy. But the programs running on
my machine are things of beauty. :-)

--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Re: Weird MIN behavior [message #42717 is a reply to message #42716] Mon, 21 February 2005 16:49 Go to previous message
Michael Wallace is currently offline  Michael Wallace
Messages: 409
Registered: December 2003
Senior Member
>> backwards. In every other language, arrays and array elements are
>> passed by reference and primitives are passed by value.
>
>
> The two languages I'm most familiar with are C and C++. In both of
> those languages, whether an array element is passed by reference only
> if it has an array type. If the element type is a scalar, it's passed
> by value, not by reference.
>

Ah, yes. I misspoke. A lot of the time I work with the arrays elements
as pointers, so I just begin to think that everything is a pointer. But
yes, if an array element is a scalar, it will be passed by value.

-Mike
Re: Weird MIN behavior [message #42720 is a reply to message #42717] Mon, 21 February 2005 16:13 Go to previous message
James Kuyper is currently offline  James Kuyper
Messages: 425
Registered: March 2000
Senior Member
Michael Wallace wrote:
...
> backwards. In every other language, arrays and array elements are
> passed by reference and primitives are passed by value.

The two languages I'm most familiar with are C and C++. In both of
those languages, whether an array element is passed by reference only
if it has an array type. If the element type is a scalar, it's passed
by value, not by reference.
Re: Weird MIN behavior [message #42741 is a reply to message #42720] Sun, 20 February 2005 17:56 Go to previous message
Michael Wallace is currently offline  Michael Wallace
Messages: 409
Registered: December 2003
Senior Member
>> In every other language, arrays and array elements are
>> passed by reference and primitives are passed by value.
>
>
> Really!? Yet one more reason to put off learning C++. :-)

Yes. Really. In C/C++, primitives are sent by value. If you need to
modify the value from within the function, you have to send the
reference to the variable instead. Arrays are automatically passed by
reference. In fact, it's possible to work with an array just like it's
a contiguous series of references.

Java is similar, except all primitive variables are passed by value.
Always. There's no way around this. All objects (including arrays) are
sent by reference.

Other languages, except IDL, have similar ideas when it comes to pass by
reference and value.

-Mike
Re: Weird MIN behavior [message #42742 is a reply to message #42741] Sun, 20 February 2005 17:31 Go to previous message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
Michael Wallace writes:

> In every other language, arrays and array elements are
> passed by reference and primitives are passed by value.

Really!? Yet one more reason to put off learning C++. :-)

Cheers,

David

--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Re: Weird MIN behavior [message #42743 is a reply to message #42742] Sun, 20 February 2005 17:12 Go to previous message
Michael Wallace is currently offline  Michael Wallace
Messages: 409
Registered: December 2003
Senior Member
Mark Hadfield wrote:
> Michael Wallace wrote:
>
>> Can someone explain this?
>>
>>
>> Normal case; everything works as you expect.
>>
>> IDL> a = [1.0, 2.0, 3.0, 4.0, 5.0]
>> IDL> b = min(a, MAX = c)
>> IDL> print, b, c
>> 1.00000 5.00000
>>
>>
>> Abnormal case; why doesn't this work?
>>
>> IDL> a = [1.0, 2.0, 3.0, 4.0, 5.0]
>> IDL> r = dblarr(2)
>> IDL> r[0] = min(a, MAX = r[1])
>> % MIN: Expression must be named variable in this context: <DOUBLE ((
>> 0.00000, 0.00000))>.
>
>
> It's complaining about the argument passed to the MAX keyword. As it
> says, this must be a named variable, and a subscripted variable does not
> qualify because it is passed by value. You're lucky that IDL tells you
> that it doesn't accept a subscripted variable here--in other situations
> it quitely leaves the value unmodified and let's you puzzle it out later.

Thanks. I had no clue what the message meant by "named variable." You
wouldn't believe how long I stared at that error message trying to
figure out what it meant. I knew things would work when I used
non-subscripted variables, but the message still didn't make sense.
Now, the message could have said "Look you idiot, a subscripted variable
is sent by value, so the function can't modify the value. This probably
isn't what you want." Now that makes sense. ;-)

I guess I had gotten it into my head that since you can change primitive
values within a function by using KEYWORD = value, you must also be able
to change array elements as well. It shouldn't surprise me that IDL is
backwards. In every other language, arrays and array elements are
passed by reference and primitives are passed by value.

This will get added to my ever growing list of gotchas.

-Mike
Re: Weird MIN behavior [message #42745 is a reply to message #42743] Sun, 20 February 2005 14:42 Go to previous message
Mark Hadfield is currently offline  Mark Hadfield
Messages: 783
Registered: May 1995
Senior Member
Michael Wallace wrote:
> Can someone explain this?
>
>
> Normal case; everything works as you expect.
>
> IDL> a = [1.0, 2.0, 3.0, 4.0, 5.0]
> IDL> b = min(a, MAX = c)
> IDL> print, b, c
> 1.00000 5.00000
>
>
> Abnormal case; why doesn't this work?
>
> IDL> a = [1.0, 2.0, 3.0, 4.0, 5.0]
> IDL> r = dblarr(2)
> IDL> r[0] = min(a, MAX = r[1])
> % MIN: Expression must be named variable in this context: <DOUBLE ((
> 0.00000, 0.00000))>.

It's complaining about the argument passed to the MAX keyword. As it
says, this must be a named variable, and a subscripted variable does not
qualify because it is passed by value. You're lucky that IDL tells you
that it doesn't accept a subscripted variable here--in other situations
it quitely leaves the value unmodified and let's you puzzle it out later.

See IDL documentation on "Parameter Passing Mechanism". Also David
Fanning's site has relevant material, eg see the info about the correct
use of the ARG_PRESENT function in

http://www.dfanning.com/tips/keyword_check.html

and also

http://www.dfanning.com/tips/read_subscripted_array.html


--
Mark Hadfield "Ka puwaha te tai nei, Hoea tatou"
m.hadfield@niwa.co.nz
National Institute for Water and Atmospheric Research (NIWA)
Re: Weird MIN behavior [message #42746 is a reply to message #42745] Sun, 20 February 2005 14:28 Go to previous message
David G. Grier is currently offline  David G. Grier
Messages: 8
Registered: September 1997
Junior Member
Hi There,

Michael Wallace wrote:

> Can someone explain this?

[snip]

> Abnormal case; why doesn't this work?
>
> IDL> a = [1.0, 2.0, 3.0, 4.0, 5.0]
> IDL> r = dblarr(2)
> IDL> r[0] = min(a, MAX = r[1])
> % MIN: Expression must be named variable in this context: <DOUBLE ((
> 0.00000, 0.00000))>.

The error message says it all: r[1] is an element of an array
rather than a variable. Here's a version that works:

IDL> r[0] = min(a, MAX = b)
IDL> r[1] = b

TTFN,

David
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: widget_base alignment question
Next Topic: Maximze a minimize GUI

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

Current Time: Fri Oct 10 11:42:09 PDT 2025

Total time taken to generate the page: 1.11832 seconds