How to insert any string after var into parse("

julia> dzien
5
julia> kanal
1

julia> parse(“Sw$dzien$kanal=read(file,"Sw$dzien")”)
:(Sw51 = read(file,“Sw5”))
is OK , but I need:

:(Sw5_1 = read(file,“Sw5”))

how to insert string “_” afeter “5” ?

julia> parse(“Sw$dzien_$kanal=read(file,"Sw$dzien")”)
ERROR: UndefVarError: dzien_ not defined

Paul

parse("""Sw$(dzien)_$(kanal)=read(file,"Sw$(dzien)")""")

use brackets

1 Like

Also your last term did not work for me. This works:
parse("Sw$(dzien)_$kanal=read(file,$("Sw$dzien"))")

Please quote your code with backticks! Like so:

```
julia> dzien
5
julia> kanal
1

julia> parse(“Sw$dzien$kanal=read(file,“Sw$dzien”)”)
:(Sw51 = read(file,“Sw5”))
is OK , but I need:

:(Sw5_1 = read(file,“Sw5”))

how to insert string “_” afeter “5” ?

julia> parse(“Sw$dzien_$kanal=read(file,“Sw$dzien”)”)
ERROR: UndefVarError: dzien_ not defined
```

Even though you don’t say what you are trying to achieve, dynamically constructing variables is almost always a bad idea (with a few exceptions, like the @load macros of JLD and similar).

You may be better off with something like

make_filename(dzien) = "Sw" * dzien
make_id(dzien, kanal) = (dzien, kanal) # could use a specific struct
d = Dict()
d[make_id(dzien, kanal)] = read(file, make_filename(dzien))

Wrapping the last line in a function and making that Dict typed would lead to nicely structured code.

1 Like

Thanks to All
A found next way :

also: \ is making string…

_ = _

parse(“Sw$dzien_$kanal=read(file,"Sw$kanal")”)
:(Sw1_5 = read(file,“Sw5”))

Paul

W dniu 2017-11-21 o 15:14, Tamas Papp pisze: