Thanks. My real error was to use @sprintf macro, thus a follow up question,
why this isn't printing anything:
macro Write(arr)
quote
for i in $arr
@sprintf("%12.6f\n",i)
end
end
end
a = 1e5*rand(10)
@Write a
This is purely for learning purposes. This was simple enough example, which
I could try to develop myself.
On Thursday, July 23, 2015 at 1:21:04 AM UTC+3, Ismael VC wrote:
>
> You forgot to interpolate the expression with `$`:
>
> julia> macro write(arr)
>
> quote
>
> for i in $arr
>
> @printf("%12.6f\n",i)
>
> end
>
> end
>
> end
>
>
> julia> a = 1e5*rand(10)
>
> 10-element Array{Float64,1}:
>
> 46310.6
>
> 25130.5
>
> 30710.8
>
> 82089.6
>
> 48240.2
>
> 80307.5
>
> 62870.3
>
> 78309.3
>
> 63086.6
>
> 86144.5
>
>
> julia> @write a
>
> 46310.583123
>
> 25130.507159
>
> 30710.765317
>
> 82089.565630
>
> 48240.227962
>
> 80307.529256
>
> 62870.334927
>
> 78309.327456
>
> 63086.608038
>
> 86144.524017
>
>
> julia>
>
> But this can be done with anormal function.
>
> El miércoles, 22 de julio de 2015, 13:37:28 (UTC-5), Tero Frondelius
> escribió:
>>
>> I'm trying to learn macros. Can you help me to get this working?
>> Currently the error is that arr is not defined. Probably an obvious
>> mistake, but I just don't get hang of it.
>>
>> macro Write(arr)
>> @eval begin
>> for i in arr
>> @sprintf("%12.6f\n",i)
>> end
>> end
>> end
>>
>>
>> a = 1e5*rand(10)
>> @Write a
>>
>>
>>
>> On Tuesday, July 14, 2015 at 4:20:43 PM UTC+3, [email protected] wrote:
>>>
>>> You could just use a macro to take the format and the array and let it
>>> write the "messy" loop for you.
>>>
>>> On Tuesday, July 14, 2015 at 8:39:44 PM UTC+10, Ferran Mazzanti wrote:
>>>>
>>>> Yes thanks, I knew already looped solutions :)
>>>> I was looking for somethin' compact as in the fortran statement above,
>>>> though. It makes things more *neat*, if there's any such thing.
>>>>
>>>> On Tuesday, July 14, 2015 at 12:08:59 PM UTC+2, Kaj Wiik wrote:
>>>>>
>>>>> Would this work for you:
>>>>> julia> a = 1e5*rand(1000)
>>>>> julia> for i in a
>>>>> @printf("%12.6f\n", i)
>>>>> end
>>>>> 74708.038385
>>>>> 71244.774457
>>>>> 5057.229038
>>>>> 3761.297034
>>>>> ...
>>>>>
>>>>> Remember that loops are fast in Julia...
>>>>>
>>>>> Kaj
>>>>>
>>>>>
>>>>>
>>>>> On Tuesday, July 14, 2015 at 9:14:37 AM UTC+3, Ferran Mazzanti wrote:
>>>>>>
>>>>>> Thanks for the info. Actually my question comes from old fortran
>>>>>> style, where I can write something of the form
>>>>>> Write(1,'1000f12.6') a
>>>>>> where a is an array. The string inside the write function says I can
>>>>>> print 1000 doubkes in 12 characters with 6 decimals. So the string is a
>>>>>> constant literal, and array a can contain 1000 or less elements that
>>>>>> will
>>>>>> be properly formatted. Is there a way to do something like this in Julia?
>>>>>> What if Inwant to print 1000 float64 on the same line with a given
>>>>>> format for each element?
>>>>>> Maybebthis is easier...
>>>>>> Best regards and thanks.
>>>>>> Ferran.
>>>>>
>>>>>