# Call different functions depending on input

**URL:** <https://discourse.julialang.org/t/call-different-functions-depending-on-input/112042>\
**Category:** General Usage\
**Created:** [March 24, 2024, 1:45pm UTC](https://discourse.julialang.org/t/call-different-functions-depending-on-input/112042 "2024-03-24T13:45:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![phyxmeow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phyxmeow/32/36373_2.png) [@phyxmeow](https://discourse.julialang.org/u/phyxmeow)\
**Post date:** [March 24, 2024, 1:45pm UTC](https://discourse.julialang.org/t/call-different-functions-depending-on-input/112042/1 "2024-03-24T13:45:12Z")

</div>

Hello! I’m writing some code including call different functions depend on whether the input string match certain pattern. Currently I’m writing code like this:

```julia
struct MyOperation
    fun::Symbol
    re::Regex
end

const operations = Vector{MyOperation}(some long list...)

function do_work(str::String)
    for op in operations
        if match(op.re ,str) !== nothing
            fun = eval(op.fun)
            fun(str)
        end
    end
end

```

But involving `eval` is clearly not good for performance. Is there a better way to do this? Thanks for any help.

---

<div class="post-metadata">

**Author:** ![screw\_dog](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/screw_dog/32/48119_2.png) [@screw\_dog](https://discourse.julialang.org/u/screw_dog)\
**Post date:** [March 24, 2024, 1:52pm UTC](https://discourse.julialang.org/t/call-different-functions-depending-on-input/112042/2 "2024-03-24T13:52:34Z")

</div>

You can just assign the function directly:

```julia
struct MyOperation
  fun::Function
  re::Regex
end

const operations = Vector{MyOperation}(some long list...)

function do_work(str::String)
    for op in operations
        if match(op.re ,str) !== nothing
            op.fun(str)
        end
    end
end

```

---

<div class="post-metadata">

**Author:** ![phyxmeow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phyxmeow/32/36373_2.png) [@phyxmeow](https://discourse.julialang.org/u/phyxmeow)\
**Post date:** [March 24, 2024, 2:09pm UTC](https://discourse.julialang.org/t/call-different-functions-depending-on-input/112042/3 "2024-03-24T14:09:13Z")

</div>

Oh… You are so right.

---

<div class="post-metadata">

**Author:** ![matthias314](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@matthias314](https://discourse.julialang.org/u/matthias314)\
**Post date:** [March 24, 2024, 2:55pm UTC](https://discourse.julialang.org/t/call-different-functions-depending-on-input/112042/4 "2024-03-24T14:55:45Z")

</div>

Depending on the application, it may be too restrictive to require `fun` to be of type `Function`. One can apply other objects to strings, too. A built-in example is

```julia
julia> Symbol("xyz")
:xyz

```
