Logo

Programming-Idioms

Execute different procedures foo, bar, baz and barfl if the string str contains the name of the respective procedure. Do it in a way natural to the language.
Implementation
Fortran

Implementation edit is for fixing errors and enhancing with metadata. Please do not replace the code below with a different implementation.

Instead of changing the code of the snippet, consider creating another Fortran implementation.

Be concise.

Be useful.

All contributions dictatorially edited by webmasters to match personal tastes.

Please do not paste any copyright violating material.

Please try to avoid dependencies to third-party libraries and frameworks.

Other implementations
case str of
  'foo': foo;
  'bar': bar;
  'baz': baz;
  'barfl': barfl;
end;
my %proc = map { $_ => \&$_ } qw(foo bar baz barfl);
$proc{$str}->() if exists $proc{$str};
Imports Microsoft.VisualBasic
CallByName(receiver, str, CallType.Method)
switch str {
case "foo":
	foo()
case "bar":
	bar()
case "baz":
	baz()
case "barfl":
	barfl()
}
switch (str)
{
    case nameof(Foo):
        Foo();
        break;
    case nameof(Bar):
        Bar();
        break;
    case nameof(Baz):
        Baz();
        break;
    case nameof(Barfl):
        Barfl();
        break;
}
match str {
    "foo" => foo(),
    "bar" => bar(),
    "baz" => baz(),
    "barfl" => barfl(),
    _ => {}
}
method(str).call if ["foo", "bar", "baz", "barfl"].include?(str)
switch = {'foo': foo, 
	'bar': bar, 
	'baz': baz, 
	'barfl': barfl
	}

switch_funct = switch.get(string)
if switch_funct : switch_funct()
switch (str) {
  case "foo":
    foo();
    break;
  case "bar":
    bar();
    break;
  case "baz":
    baz();
    break;
  case "barfl":
    barfl();
    break;
}
(some-> str {"foo" foo "bar" bar "baz" baz "barfl" barfl} (.call))
(def whitelist #{#'foo #'bar #'baz #'barfl})
(some-> str symbol resolve whitelist (.call))
switch ($str) {
  case "foo":
    foo();
    break;
  case "bar":
    bar();
    break;
  case "baz":
    baz();
    break;
  case "barfl":
    barfl();
    break;
}
IDENTIFICATION DIVISION.
PROGRAM-ID. reverse string.
PROCEDURE DIVISION.
    EVALUATE str
       WHEN foo
	  PERFORM foo
       WHEN bar
          PERFORM bar
       WHEN baz
	  PERFORM baz 
       WHEN barfl
	  PERFORM barfl
       WHEN OTHER
	  CONTINUE
    END-EVALUATE.	
STOP RUN.
case str of
  "foo" -> foo
  "bar" -> bar
  "baz" -> baz
  "barfl" -> barfl
Select Case str
    Case NameOf(Foo)
        Foo()
    Case NameOf(Bar)
        Bar()
    Case NameOf(Baz)
        Baz()
    Case NameOf(Barfl)
        Barfl()
End Select
switch (str){
	case "bar":
            bar();
            break;
        case "baz":
            baz();
            break;
        case "foo":
            foo();
            break;
        case "barfl":
            barfl();
            break;
        default:
            somethingElse();
    }
var myProc = {"foo": foo, 
              "bar": bar, 
              "baz": baz, 
              "barfl": barfl};

myProc[str]?.call();