# Write function in loop

**URL:** <https://discourse.julialang.org/t/write-function-in-loop/62121>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [May 31, 2021, 9:19am UTC](https://discourse.julialang.org/t/write-function-in-loop/62121 "2021-05-31T09:19:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![assia855](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/assia855/32/22845_2.png) [@assia855](https://discourse.julialang.org/u/assia855)\
**Post date:** [May 31, 2021, 9:19am UTC](https://discourse.julialang.org/t/write-function-in-loop/62121/1 "2021-05-31T09:19:54Z")

</div>

I would like the output files of my program to have the same label as the input files. So I used that bellow code, but it doesn’t seems that it’s working.

```julia
name = name_data[12:-6]

write(ML, "myResults/Separable_Linear_"*name*".fits")

```

Here’s it the name of one of my name\_data: “Model\_Synthetic\_Star\_0.2\_incl=90\_pa=180\_e=0\_wave=90”.

Thank you in advance

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [May 31, 2021, 9:40am UTC](https://discourse.julialang.org/t/write-function-in-loop/62121/2 "2021-05-31T09:40:46Z")

</div>

> [@assia855](#):
>
> `name = name_data[12:-6]`

That way of indexing as in Python does not work in Julia. You should use `end-n` to refer to the last but n-th element.

---

<div class="post-metadata">

**Author:** ![assia855](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/assia855/32/22845_2.png) [@assia855](https://discourse.julialang.org/u/assia855)\
**Post date:** [May 31, 2021, 9:54am UTC](https://discourse.julialang.org/t/write-function-in-loop/62121/3 "2021-05-31T09:54:13Z")

</div>

Thank you I got.  
Here’s the solution that I found out.  
name\_data=“model/THENAMEOFYOURFILE.fits”, “model/” is composed of 6 char, so I should take [7:end-6]

```julia

name = name_data[7:end-6]

write(ML, "myResults/Separable_"*name*".fits")

```

Like that I got the same label as the input files as the output files.

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [May 31, 2021, 10:04am UTC](https://discourse.julialang.org/t/write-function-in-loop/62121/4 "2021-05-31T10:04:11Z")

</div>

Ok, you can also get the parts of the path and file names independently from their actual length with functions as `splitpath`, `splitdir` or `splitext`.

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [May 31, 2021, 10:07am UTC](https://discourse.julialang.org/t/write-function-in-loop/62121/5 "2021-05-31T10:07:21Z")

</div>

And this package seems also very well suited to your use case

> **[GitHub - JuliaDynamics/DrWatson.jl: The perfect sidekick to your scientific...](https://github.com/JuliaDynamics/DrWatson.jl)**
>
> The perfect sidekick to your scientific inquiries. Contribute to JuliaDynamics/DrWatson.jl development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![lawless-m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lawless-m/32/30869_2.png) [@lawless-m](https://discourse.julialang.org/u/lawless-m)\
**Post date:** [January 13, 2022, 9:11am UTC](https://discourse.julialang.org/t/write-function-in-loop/62121/6 "2022-01-13T09:11:43Z")

</div>

Not really relevant to your question but rather than creating a variable to contain the string section and then another new string for the filename, you can use interpolation directly on the original string

`"myResults/Separable_Linear_$(name_data[7:end-6]).fits"`
