HP Forums
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:

  CASE // call constants for planet
  IF nameplanet:="Mercurius" THEN Mercurius(); BREAK; END;
  IF nameplanet:="Venus" THEN Venus();BREAK; END;
  IF nameplanet:="Mars" THEN Mars(); BREAK;END;
  IF nameplanet:="Jupiter" THEN Jupiter(); BREAK; END;
  IF nameplanet:="Saturn" THEN Saturn(); BREAK; END;
  IF nameplanet:="Uranus" THEN Uranus(); BREAK; END;
  IF nameplanet:="Neptunus" THEN Neptunus();BREAK;  END;
  IF nameplanet:="Pluto" THEN Pluto(); BREAK; END;
  DEFAULT data();
  END; // case

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:

IF nameplanet =="Mercurius" THEN Mercurius(); BREAK; END;

...

Let me know what happens.

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):

Code:
EVAL(EXPR(nameplanet+"()"));

thank you, I had tried
Code:
EVAL(nameplanet())
but it was wrong.
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.
Other languages would use an associative array (string, function), or even simply an array in some cases.

sure, but speed is "no problem" in my case, with ...8 function called and everyone with 20 lines of code...
Here it works well Smile

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.
Other languages would use an associative array (string, function), or even simply an array in some cases.
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 {
    print "Mercurius\n";
}

sub Venus {
    print "Venus\n";
}

sub Mars {
    print "Mars\n";
}

sub Jupiter {
    print "Jupiter\n";
}

sub Saturn {
    print "Saturn\n";
}

sub Uranus {
    print "Uranus\n";
}

sub Neptunus {
    print "Neptunus\n";
}

sub Pluto {
    print "Pluto\n";
}

my %constant = (
    'Mercurius' => \&Mercurius,
    'Venus'     => \&Venus,
    'Mars'      => \&Mars,
    'Jupiter'   => \&Jupiter,
    'Saturn'    => \&Saturn,
    'Uranus'    => \&Uranus,
    'Neptunus'  => \&Neptunus,
    'Pluto'     => \&Pluto,
);

my $nameplanet = 'Uranus';
$constant{$nameplanet}->();

Python
Code:
def Mercurius():
     print 'Mercurius'

def Venus():
     print 'Venus'

def Mars():
     print 'Mars'

def Jupiter():
     print 'Jupiter'

def Saturn():
     print 'Saturn'

def Uranus():
     print 'Uranus'

def Neptunus():
     print 'Neptunus'

def Pluto():
     print 'Pluto'

constant = {
    'Mercurius' : Mercurius,
    'Venus'     : Venus,
    'Mars'      : Mars,
    'Jupiter'   : Jupiter,
    'Saturn'    : Saturn,
    'Uranus'    : Uranus,
    'Neptunus'  : Neptunus,
    'Pluto'     : Pluto,
}

nameplanet = 'Uranus'
constant[nameplanet]()

Cheers
Thomas


RE: Dynamically call a function - DrD - 06-21-2015 08:03 PM

Did you try EXPR() w/o EVAL?:

Code:

Nibiru();
EXPORT test()
BEGIN
   nameplanet:="Nibiru";
   EXPR(nameplanet);     // Evaluates EXPRession  
end;

Nibiru()                 // Parameter list omitted
begin
  msgbox("Planet X");
end;



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:

planets()
BEGIN
  LOCAL ch, nameplanet;
  INPUT({{ch,{"Mercurius","Venus","Mars", "Jupiter", "Saturn", "Uranus", "Neptunus", "Pluto"}, {20,30,1}}},
  "Choose planet", 
  "planet: ", "Choose the planet to calculate an press OK", 0,5 );
  CASE
  IF ch==1 THEN nameplanet:="Mercurius"; planetCalc(nameplanet); END;
  IF ch==2 THEN nameplanet:="Venus"; planetCalc(nameplanet); END;
  ...
  DEFAULT
  END; // case
END;

...
EVAL(EXPR(nameplanet+"()"));
...

Mercurius()
BEGUN
...
END;
it works with a list of planet passing it the name of function to call.


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()
BEGIN
  LOCAL ch, nameplanet:={"Mercurius","Venus","Mars", "Jupiter", "Saturn", "Uranus", "Neptunus", "Pluto"};
  INPUT({{ch, nameplanet, {20,30,1}}},
  "Choose planet", 
  "planet: ", "Choose the planet to calculate an press OK", 0,5 );
  IF ch THEN EXPR(nameplanet(ch)+"()"); END;
END;



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:

Code:
planets()
BEGIN
  LOCAL ch, nameplanet:={"Mercurius","Venus","Mars", "Jupiter", "Saturn", "Uranus", "Neptunus", "Pluto"};
  INPUT({{ch, nameplanet, {20,30,1}}},
  "Choose planet", 
  "planet: ", "Choose the planet to calculate an press OK", 0,5 );
  IF ch THEN EXPR(nameplanet(ch)+"()"); END;
END;

thank you!
I'll try soon...