Dynamically call a function
|
06-20-2015, 03:47 PM
Post: #1
|
|||
|
|||
Dynamically call a function
hi,
I want call a function dynamically, i.e. nameplanet() to call Mercurius(), Saturnus()... giving a variable: nameplanet (:= "Mercuriius" ...) This code doesn't work: Code:
It gives "Bad argument type" in my program... Any hint? Thanks Salvo ∫aL√0mic (IT9CLU) :: HP Prime 50g 41CX 71b 42s 39s 35s 12C 15C - DM42, DM41X - WP34s Prime Soft. Lib |
|||
06-20-2015, 04:06 PM
Post: #2
|
|||
|
|||
RE: Dynamically call a function
Try:
IF nameplanet =="Mercurius" THEN Mercurius(); BREAK; END; not: IF nameplanet:="Mercurius" THEN Mercurius(); BREAK; END; and a similar change for the other cases. Let me know what happens. Road |
|||
06-20-2015, 05:01 PM
Post: #3
|
|||
|
|||
RE: Dynamically call a function
(06-20-2015 04:06 PM)roadrunner Wrote: Try: hi Road, you're right! Today I'm to tired, hi... Sure, I was assigning a value instead of confront it... yes, now it's ok! thank you Salvo ∫aL√0mic (IT9CLU) :: HP Prime 50g 41CX 71b 42s 39s 35s 12C 15C - DM42, DM41X - WP34s Prime Soft. Lib |
|||
06-20-2015, 05:43 PM
Post: #4
|
|||
|
|||
RE: Dynamically call a function
You can also use (preferentially if nameplanet is not set by the user):
Code: EVAL(EXPR(nameplanet+"()")); My website: ried.cl |
|||
06-20-2015, 05:56 PM
Post: #5
|
|||
|
|||
RE: Dynamically call a function
(06-20-2015 05:43 PM)eried Wrote: You can also use (preferentially if nameplanet is not set by the user): thank you, I had tried Code: EVAL(nameplanet()) I'll try also this, now! Salvo ∫aL√0mic (IT9CLU) :: HP Prime 50g 41CX 71b 42s 39s 35s 12C 15C - DM42, DM41X - WP34s Prime Soft. Lib |
|||
06-21-2015, 07:23 AM
Post: #6
|
|||
|
|||
RE: Dynamically call a function
eval() is a powerful capability of dynamic languages, but it bears high speed cost, so it's usually left to non-speed-critical code, as is probably the case here.
Other languages would use an associative array (string, function), or even simply an array in some cases. |
|||
06-21-2015, 12:55 PM
Post: #7
|
|||
|
|||
RE: Dynamically call a function
(06-21-2015 07:23 AM)debrouxl Wrote: eval() is a powerful capability of dynamic languages, but it bears high speed cost, so it's usually left to non-speed-critical code, as is probably the case here. sure, but speed is "no problem" in my case, with ...8 function called and everyone with 20 lines of code... Here it works well Salvo ∫aL√0mic (IT9CLU) :: HP Prime 50g 41CX 71b 42s 39s 35s 12C 15C - DM42, DM41X - WP34s Prime Soft. Lib |
|||
06-21-2015, 03:59 PM
Post: #8
|
|||
|
|||
RE: Dynamically call a function
(06-21-2015 07:23 AM)debrouxl Wrote: eval() is a powerful capability of dynamic languages, but it bears high speed cost, so it's usually left to non-speed-critical code, as is probably the case here.Can you give a coding example of the associative array please. Cheers, Hamilton. |
|||
06-21-2015, 04:44 PM
Post: #9
|
|||
|
|||
RE: Dynamically call a function
(06-21-2015 03:59 PM)hamilton.milligan@outlook.com Wrote: Can you give a coding example of the associative array please. Both programs just print 'Uranus'. Perl Code: sub Mercurius { Python Code: def Mercurius(): Cheers Thomas |
|||
06-21-2015, 08:03 PM
Post: #10
|
|||
|
|||
RE: Dynamically call a function
Did you try EXPR() w/o EVAL?:
Code:
|
|||
06-22-2015, 07:13 AM
(This post was last modified: 06-22-2015 07:16 AM by salvomic.)
Post: #11
|
|||
|
|||
RE: Dynamically call a function
(06-21-2015 08:03 PM)DrD Wrote: Did you try EXPR() w/o EVAL?: ok, yes. For now the simplest way, in my case, was the Eried tip: Code:
∫aL√0mic (IT9CLU) :: HP Prime 50g 41CX 71b 42s 39s 35s 12C 15C - DM42, DM41X - WP34s Prime Soft. Lib |
|||
06-22-2015, 08:06 AM
(This post was last modified: 06-22-2015 08:12 AM by Didier Lachieze.)
Post: #12
|
|||
|
|||
RE: Dynamically call a function
You can also avoid the CASE ... END; with something like this:
Code: planets() |
|||
06-22-2015, 12:43 PM
Post: #13
|
|||
|
|||
RE: Dynamically call a function
(06-22-2015 08:06 AM)Didier Lachieze Wrote: You can also avoid the CASE ... END; with something like this: thank you! I'll try soon... ∫aL√0mic (IT9CLU) :: HP Prime 50g 41CX 71b 42s 39s 35s 12C 15C - DM42, DM41X - WP34s Prime Soft. Lib |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 3 Guest(s)