Dynamically call a function - Printable Version +- HP Forums (https://www.hpmuseum.org/forum) +-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html) +--- Forum: HP Prime (/forum-5.html) +--- Thread: Dynamically call a function (/thread-4193.html) |
Dynamically call a function - salvomic - 06-20-2015 03:47 PM 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 RE: Dynamically call a function - roadrunner - 06-20-2015 04:06 PM 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 RE: Dynamically call a function - salvomic - 06-20-2015 05:01 PM (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 RE: Dynamically call a function - eried - 06-20-2015 05:43 PM You can also use (preferentially if nameplanet is not set by the user): Code: EVAL(EXPR(nameplanet+"()")); RE: Dynamically call a function - salvomic - 06-20-2015 05:56 PM (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 RE: Dynamically call a function - debrouxl - 06-21-2015 07:23 AM 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. RE: Dynamically call a function - salvomic - 06-21-2015 12:55 PM (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 RE: Dynamically call a function - hamilton.milligan@outlook.com - 06-21-2015 03:59 PM (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. RE: Dynamically call a function - Thomas Klemm - 06-21-2015 04:44 PM (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 RE: Dynamically call a function - DrD - 06-21-2015 08:03 PM Did you try EXPR() w/o EVAL?: Code:
RE: Dynamically call a function - salvomic - 06-22-2015 07:13 AM (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:
RE: Dynamically call a function - Didier Lachieze - 06-22-2015 08:06 AM You can also avoid the CASE ... END; with something like this: Code: planets() RE: Dynamically call a function - salvomic - 06-22-2015 12:43 PM (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... |