Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Entering the format string as a variable throws error #15

Closed
Kevin-Mattheus-Moerman opened this issue Dec 20, 2023 · 3 comments
Closed

Comments

@Kevin-Mattheus-Moerman
Copy link

Kevin-Mattheus-Moerman commented Dec 20, 2023

The following works:

@scanf("1.23456", "%f",Float64)

But the following throws an error:

f = "%f"
@scanf("1.23456", f,Float64)
ERROR: ArgumentError: mismatch between # of format specifiers and provided args: 0 != 2
Stacktrace:
 [1] throwa(msg::String)
   @ Scanf ~/.julia/packages/Scanf/5ncBL/src/Scanf.jl:833
 [2] argmismatch(a::Int64, b::Int64)
   @ Scanf ~/.julia/packages/Scanf/5ncBL/src/Scanf.jl:830
 [3] scanner(::BufferedStreams.BufferedInputStream{Base.TTY}, ::Scanf.Format{Base.CodeUnits{UInt8, String}, Tuple{Scanf.LiteralSpec{SubString{String}}}}, ::String, ::Vararg{Any})
   @ Scanf ~/.julia/packages/Scanf/5ncBL/src/Scanf.jl:647
 [4] scanf(::BufferedStreams.BufferedInputStream{Base.TTY}, ::Scanf.Format{Base.CodeUnits{UInt8, String}, Tuple{Scanf.LiteralSpec{SubString{String}}}}, ::String, ::Vararg{Any})
   @ Scanf ~/.julia/packages/Scanf/5ncBL/src/Scanf.jl:126
 [5] scanf(::Base.TTY, ::Scanf.Format{Base.CodeUnits{UInt8, String}, Tuple{Scanf.LiteralSpec{SubString{String}}}}, ::String, ::Vararg{Any})
   @ Scanf ~/.julia/packages/Scanf/5ncBL/src/Scanf.jl:127
 [6] top-level scope
   @ REPL[89]:1

The context is that I'd like the format to be variable, in some cases f="%f, %f", %f" for instance. Or f="%d, %f", %f, %f, %f" .

How can I programmatically enter the format string?

In addition it would be great to replicate the second part that says Float64 n-times, e.g. if f="%f"^n. Any pointers how to do that? Thanks.

@KlausC
Copy link
Owner

KlausC commented Dec 20, 2023

You are right, @scanf only supports string constants as formats, but you can use one of:

julia> f = Scanf.Format("%f") # also variables possible
Scanf.format"%f"

julia> scanf("12.34", f, 1.5)
(1, 12.34)

julia> f = Scanf.format"%f" # only constants
Scanf.format"%f"

julia> scanf("12.34", f, 1.5)
(1, 12.34)

@KlausC
Copy link
Owner

KlausC commented Dec 20, 2023

For your second question:

julia> n = 5
5

julia> f = Scanf.Format("%f"^n)
Scanf.format"%f%f%f%f%f"

julia> scanf("1 2 3 4 5 6 7 8", f, zeros(5)...)
(5, 1.0, 2.0, 3.0, 4.0, 5.0)

but be aware, that calling Scanf.Format has to interpret the format string each time it is called. So that should not be done inside a hot loop.

@Kevin-Mattheus-Moerman
Copy link
Author

@KlausC thanks for getting back to me, and for sharing this solution. 🎄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants