procedure in case statement [message #82049] |
Mon, 19 November 2012 05:36  |
hannah_ue
Messages: 14 Registered: November 2012
|
Junior Member |
|
|
hi there,
i'm relatively new to IDL programming. is there any way to run a PROcedure in a CASE statement? i want to ask myself via READ which PROcedure i want to use.
thanks for your help!
ich
|
|
|
Re: procedure in case statement [message #82130 is a reply to message #82049] |
Mon, 19 November 2012 12:13  |
Russell Ryan
Messages: 122 Registered: May 2012
|
Senior Member |
|
|
On Monday, November 19, 2012 10:57:00 AM UTC-5, hann...@web.de wrote:
> thanks a lot, paulv and DavidF. now it works.
>
> i'm sorry if my question was formulated kind of inconvenient and ineffectively - it was my first post and i will work on that.
>
> hannah
Hi Hannah,
Yeah the problem your having is that IDL will default a datatype as a float, and you're trying to assign it a float. So, you need to explicitly set the type to string before hand. When I do this, I usually do:
ans='proc1' ; or the default setting
read,'Which procedure do you want to run: ',ans
case ans of
'proc1':
'proc2':
endcase
But, this can be very dangerous for a slew of reasons. (1) what if the user misspells the procedure? (2) what if they type something that's disallowed? (which I guess is like (1)), (3) what if they capitalize? Obviously, you can see a host of problems. To guard against these things you should set default settings, have an "else" clause in your case, and check capitalization.
Good luck,
Russell
|
|
|
Re: procedure in case statement [message #82133 is a reply to message #82049] |
Mon, 19 November 2012 11:29  |
Paul Van Delst[1]
Messages: 1157 Registered: April 2002
|
Senior Member |
|
|
Completely off the original topic, but (regarding the questions URL) I'm
still grappling with the "volume is not precision" aspect. Sigh.
On the other hand, the quip:
"All diagnosticians are from Missouri."
made me nose-squirt coffee (it doesn't take much, but I thought that was
clever)
cheers,
paulv
On 11/19/12 11:22, Coyote wrote:
> Hi Hannah,
>
> You write:
>
>> i'm sorry if my question was formulated kind of inconvenient and
>> ineffectively - it was my first post and i will work on that.
>
> Well, welcome. I think you will find this newsgroup to be the largest
> collection of helpful IDL experts in the world. They LOVE to have
> newbies show up so they can strut their stuff!
>
> Here are some gentle suggestions for how to ask a question on the
> newsgroup so that it has a good chance of being answered:
>
> http://www.catb.org/~esr/faqs/smart-questions.html
>
> We are reasonably forgiving, but all very busy. A good question
> almost always gets a good answer.
>
> Cheers,
>
> David
|
|
|
Re: procedure in case statement [message #82136 is a reply to message #82049] |
Mon, 19 November 2012 08:22  |
DavidF[1]
Messages: 94 Registered: April 2012
|
Member |
|
|
Hi Hannah,
You write:
> i'm sorry if my question was formulated kind of inconvenient and ineffectively - it was my first post and i will work on that.
Well, welcome. I think you will find this newsgroup to be the largest collection of helpful IDL experts in the world. They LOVE to have newbies show up so they can strut their stuff!
Here are some gentle suggestions for how to ask a question on the newsgroup so that it has a good chance of being answered:
http://www.catb.org/~esr/faqs/smart-questions.html
We are reasonably forgiving, but all very busy. A good question almost always gets a good answer.
Cheers,
David
|
|
|
Re: procedure in case statement [message #82138 is a reply to message #82049] |
Mon, 19 November 2012 07:57  |
hannah_ue
Messages: 14 Registered: November 2012
|
Junior Member |
|
|
thanks a lot, paulv and DavidF. now it works.
i'm sorry if my question was formulated kind of inconvenient and ineffectively - it was my first post and i will work on that.
hannah
|
|
|
Re: procedure in case statement [message #82139 is a reply to message #82049] |
Mon, 19 November 2012 07:52  |
Paul Van Delst[1]
Messages: 1157 Registered: April 2002
|
Senior Member |
|
|
Hmm. Not really. This is what I get (on the command line):
IDL> read,rot,prompt='rot via proc one or two? (1 or 2)'
rot via proc one or two? (1 or 2)3
IDL> help, rot
ROT FLOAT = 3.00000
So, you can see it assumes "rot" is a float by default.
Now let's try a string:
IDL> read,rot,prompt='rot via proc one or two? (1 or 2)'
rot via proc one or two? (1 or 2)proc_one
% READ: Input conversion error. Unit: 0, File: <stdin>
% Execution halted at: $MAIN$
Oops. So, DavidF was correct. You have to cast the variable as a string
before you use it as such:
IDL> rot=""
IDL> read,rot,prompt='rot via proc one or two? (1 or 2)'
rot via proc one or two? (1 or 2)proc_one
IDL> help, rot
ROT STRING = 'proc_one'
IDL> print, !version
{ x86 linux unix linux 8.2 Apr 10 2012 32 64}
cheers,
paulv
On 11/19/12 10:46, hannah_ue@web.de wrote:
> thanks for your answer. i do it exactly like you suggested, but always get the error "read: input conversion error. unit: 0, file:<stdin>"
> do you have an idea what could be wrong?
>
> thanks, hannah
|
|
|
Re: procedure in case statement [message #82140 is a reply to message #82049] |
Mon, 19 November 2012 07:46  |
hannah_ue
Messages: 14 Registered: November 2012
|
Junior Member |
|
|
thanks for your answer. i do it exactly like you suggested, but always get the error "read: input conversion error. unit: 0, file: <stdin>"
do you have an idea what could be wrong?
thanks, hannah
|
|
|
Re: procedure in case statement [message #82141 is a reply to message #82049] |
Mon, 19 November 2012 07:44  |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
hannah_ue@web.de writes:
> so do you have any idea why i get the "read: input conversion error. unit: 0, file: <stdin>" error?
I think you will find we will have all kinds of ideas
if you share EXACTLY what the problem is (i.e., the
error message) and the code that produces it. If we
have to keep guessing, most of us will lose interest
in the game and go back to work. :-)
I would say that this problem comes about because
IDL thinks the thing you are trying to read is a
float and you keep typing a string. Something like
this might help:
ans = ""
read, ans
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 thou speakest truth.")
|
|
|
Re: procedure in case statement [message #82142 is a reply to message #82049] |
Mon, 19 November 2012 07:42  |
Paul Van Delst[1]
Messages: 1157 Registered: April 2002
|
Senior Member |
|
|
Here's what I am assuming:
1) you have a separate file called "proc_one.pro" (all lower case) that
contains the "proc_one" procedure.
2) you have a separate file called "proc_two.pro" (all lower case) that
contains the "proc_two" procedure.
Based on that, why can't you just do:
read,rot,prompt='rot via proc one or two? (1 or 2)'
case rot of
1: proc_one,a,b,c
2: proc_two,d,e,f
endcase
Note I use an integer selector rather than typing out the actual name of
the procedure required (after a couple of tests, all that typing would
drive me nuts).
Once you get this working, then you might want to start to get fancier
using CALL_PROCEDURE, or EXECUTE, or similar.
cheers,
paulv
On 11/19/12 09:06, hannah_ue@web.de wrote:
> Am Montag, 19. November 2012 14:36:19 UTC+1 schrieb hann...@web.de:
>> hi there,
>>
>> i'm relatively new to IDL programming. is there any way to run a
>> PROcedure in a CASE statement? i want to ask myself via READ which
>> PROcedure i want to use.
>>
>> thanks for your help!
>>
>> ich
>
> hm... i get the same error. what i have is: 2 procedures i wrote
> whichwork. instead of using two different files for each procedure, i want
> one file where i can choose the one procedure i like. lets call them
> proc_one,a,b,c and proc_two,d,e,f. i tried it like the following:
>
> read,rot,prompt='rot via proc one or two? (proc_one,proc_two)'
> case rot of
> 'proc_one': call_procedure,proc_one,a,b,c
> 'proc_two': call_procedure,porch_two,d,e,f
> endcase
>
> maybe this is not very clever but i have no idea how to do it in another way
|
|
|
|
|
Re: procedure in case statement [message #82145 is a reply to message #82049] |
Mon, 19 November 2012 07:33  |
hannah_ue
Messages: 14 Registered: November 2012
|
Junior Member |
|
|
> Well, there are a LOT of things wrong with this approach,
>
> probably, but I would start by putting your two procedures
>
> in two separate files, named correctly:
sorry for my misspelling!
well, i already did this
|
|
|
Re: procedure in case statement [message #82146 is a reply to message #82049] |
Mon, 19 November 2012 06:33  |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
hannah_ue@web.de writes:
> hm... i get the same error. what i have is: 2 procedures i wrote which work. instead of using two different files for each procedure, i want one file where i can choose the one procedure i like. lets call them proc_one,a,b,c and proc_two,d,e,f. i tried it like the following:
>
> read,rot,prompt='rot via proc one or two? (proc_one,proc_two)'
> case rot of
> 'proc_one': call_procedure,proc_one,a,b,c
> 'proc_two': call_procedure,porch_two,d,e,f
> endcase
>
> maybe this is not very clever but i have no idea how to do it in another way
Well, there are a LOT of things wrong with this approach,
probably, but I would start by putting your two procedures
in two separate files, named correctly:
http://www.idlcoyote.com/code_tips/mostcommon.html
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 thou speakest truth.")
|
|
|
Re: procedure in case statement [message #82147 is a reply to message #82049] |
Mon, 19 November 2012 06:06  |
hannah_ue
Messages: 14 Registered: November 2012
|
Junior Member |
|
|
Am Montag, 19. November 2012 14:36:19 UTC+1 schrieb hann...@web.de:
> hi there,
>
> i'm relatively new to IDL programming. is there any way to run a PROcedure in a CASE statement? i want to ask myself via READ which PROcedure i want to use.
>
> thanks for your help!
>
> ich
hm... i get the same error. what i have is: 2 procedures i wrote which work. instead of using two different files for each procedure, i want one file where i can choose the one procedure i like. lets call them proc_one,a,b,c and proc_two,d,e,f. i tried it like the following:
read,rot,prompt='rot via proc one or two? (proc_one,proc_two)'
case rot of
'proc_one': call_procedure,proc_one,a,b,c
'proc_two': call_procedure,porch_two,d,e,f
endcase
maybe this is not very clever but i have no idea how to do it in another way
|
|
|
Re: procedure in case statement [message #82148 is a reply to message #82049] |
Mon, 19 November 2012 05:50  |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
hannah_ue@web.de writes:
> i'm relatively new to IDL programming. is there any way to run a PROcedure in a CASE statement? i want to ask myself via READ which PROcedure i want to use.
> thanks for your help!
Use CALL_PROCEDURE.
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 thou speakest truth.")
|
|
|