HP Forums

Full Version: Dynamically call a function
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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 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
You can also use (preferentially if nameplanet is not set by the user):

Code:
EVAL(EXPR(nameplanet+"()"));
(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
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 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
(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.
(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
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;
(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.
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;
(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...
Reference URL's