Post Reply 
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:

  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

∫aL√0mic (IT9CLU) :: HP Prime 50g 41CX 71b 42s 39s 35s 12C 15C - DM42, DM41X - WP34s Prime Soft. Lib
Visit this user's website Find all posts by this user
Quote this message in a reply
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
Find all posts by this user
Quote this message in a reply
06-20-2015, 05:01 PM
Post: #3
RE: Dynamically call a function
(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

∫aL√0mic (IT9CLU) :: HP Prime 50g 41CX 71b 42s 39s 35s 12C 15C - DM42, DM41X - WP34s Prime Soft. Lib
Visit this user's website Find all posts by this user
Quote this message in a reply
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
Visit this user's website Find all posts by this user
Quote this message in a reply
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):

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

thank you, I had tried
Code:
EVAL(nameplanet())
but it was wrong.
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
Visit this user's website Find all posts by this user
Quote this message in a reply
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.
Find all posts by this user
Quote this message in a reply
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.
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

∫aL√0mic (IT9CLU) :: HP Prime 50g 41CX 71b 42s 39s 35s 12C 15C - DM42, DM41X - WP34s Prime Soft. Lib
Visit this user's website Find all posts by this user
Quote this message in a reply
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.
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.

Cheers,
Hamilton.
Find all posts by this user
Quote this message in a reply
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 {
    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
Find all posts by this user
Quote this message in a reply
06-21-2015, 08:03 PM
Post: #10
RE: Dynamically call a function
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;
Find all posts by this user
Quote this message in a reply
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:

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.

∫aL√0mic (IT9CLU) :: HP Prime 50g 41CX 71b 42s 39s 35s 12C 15C - DM42, DM41X - WP34s Prime Soft. Lib
Visit this user's website Find all posts by this user
Quote this message in a reply
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()
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;
Find all posts by this user
Quote this message in a reply
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:

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...

∫aL√0mic (IT9CLU) :: HP Prime 50g 41CX 71b 42s 39s 35s 12C 15C - DM42, DM41X - WP34s Prime Soft. Lib
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 3 Guest(s)