# For loop : mixing iterator with argument of a function

**URL:** https://discourse.julialang.org/t/for-loop-mixing-iterator-with-argument-of-a-function/85846
**Category:** General Usage
**Tags:** images, jupyter, colors, iterators, for-loop
**Created:** [August 16, 2022, 8:01pm UTC](https://discourse.julialang.org/t/for-loop-mixing-iterator-with-argument-of-a-function/85846 "2022-08-16T20:01:42Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Julia\_fun\_123](https://avatars.discourse-cdn.com/v4/letter/j/96bed5/32.png) [@Julia\_fun\_123](https://discourse.julialang.org/u/Julia_fun_123)
#### Post date: [August 16, 2022, 8:01pm UTC](https://discourse.julialang.org/t/for-loop-mixing-iterator-with-argument-of-a-function/85846/1 "2022-08-16T20:01:42Z")

</div>

Hello everyone.

I am using Julia 1.7.3 with Jupyter Lab, and I have this code (which is partially taken from this link : [Converting image colors · ColorSchemeTools](https://juliagraphics.github.io/ColorSchemeTools.jl/stable/convertingimages/#Convert-image-from-one-scheme-to-another)) :

```julia

using FileIO, ColorSchemes, ColorSchemeTools, Images

img = load("julia-logo-square.png")
img_rgb = RGB.(img)

convertedimage_4 = convert_to_scheme(ColorSchemes.PiYG_4, img_rgb)
convertedimage_5 = convert_to_scheme(ColorSchemes.PiYG_5, img_rgb)
convertedimage_6 = convert_to_scheme(ColorSchemes.PiYG_6, img_rgb)

IJulia.display(convertedimage_4)
IJulia.display(convertedimage_5)
IJulia.display(convertedimage_6)
    

```

However, instead of typing :

```julia
convertedimage_4 = convert_to_scheme(ColorSchemes.PiYG_4, img_rgb)
convertedimage_5 = convert_to_scheme(ColorSchemes.PiYG_5, img_rgb)
convertedimage_6 = convert_to_scheme(ColorSchemes.PiYG_6, img_rgb)

IJulia.display(convertedimage_4)
IJulia.display(convertedimage_5)
IJulia.display(convertedimage_6)
    

```

I’d like to execute the following pseudo-code :

```julia
for i in 4:6
    convertedimage_i = convert_to_scheme(ColorSchemes.PiYG_i, img_rgb)
    IJulia.display(convertedimage_i)
end

```

where the iterator i will go from 4 to 6 of course.

This is easier when done with strings, but in this situation, I am a bit stuck.

Anybody have an idea on how to solve this (it’s basic I think, but I’m not sure how to do this in Julia) ?

---

<div class="post-metadata">

### Author: ![Jollywatt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jollywatt/32/202198_2.png) [@Jollywatt](https://discourse.julialang.org/u/Jollywatt)
#### Post date: [August 16, 2022, 8:33pm UTC](https://discourse.julialang.org/t/for-loop-mixing-iterator-with-argument-of-a-function/85846/2 "2022-08-16T20:33:20Z")

</div>

The way you programmatically access properties is with `getproperty`, which accepts `Symbol`s.  
I.e., `ColorSchemes.PiYG_1 === getproperty(ColorSchemes, :PiYG_1)`.  
So you can do:

```julia
for i in 4:6
    scheme = getproperty(ColorSchemes, Symbol("PiYG_$i"))
    convertedimage_i = convert_to_scheme(scheme, img_rgb)
    IJulia.display(convertedimage_i)
end
```

---

<div class="post-metadata">

### Author: ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)
#### Post date: [August 16, 2022, 8:37pm UTC](https://discourse.julialang.org/t/for-loop-mixing-iterator-with-argument-of-a-function/85846/3 "2022-08-16T20:37:39Z")

</div>

I expect that the desired assignment is to variables such as `convertedimage_4` etc. In that case:

```julia
i = 4
v = Symbol("convertedimage_$i")
cs = getfield(ColorSchemes, Symbol("PiYG_$i"))
@eval ($v)=($cs)

```

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [August 16, 2022, 8:49pm UTC](https://discourse.julialang.org/t/for-loop-mixing-iterator-with-argument-of-a-function/85846/4 "2022-08-16T20:49:16Z")

</div>

> [@Jollywatt](#):
>
> `Symbol("PiYG_$i")`

It’s not really faster or anything, but I like this syntax, aesthetically:

```julia
Symbol(:PiYG_, i)

```

---

<div class="post-metadata">

### Author: ![Julia\_fun\_123](https://avatars.discourse-cdn.com/v4/letter/j/96bed5/32.png) [@Julia\_fun\_123](https://discourse.julialang.org/u/Julia_fun_123)
#### Post date: [August 16, 2022, 9:56pm UTC](https://discourse.julialang.org/t/for-loop-mixing-iterator-with-argument-of-a-function/85846/5 "2022-08-16T21:56:30Z")

</div>

Perfect ! That worked. Thanks a lot !
