iota to array
Uknown
sireeshkodali1 at gmail.com
Sun Feb 25 06:31:26 UTC 2018
On Sunday, 25 February 2018 at 06:22:03 UTC, psychoticRabbit
wrote:
> On Sunday, 25 February 2018 at 05:40:19 UTC, Jonathan M Davis
> wrote:
>>
>> int[] intArr = iota(1, 11).array();
>>
>>
>> - Jonathan M Davis
>
> thanks!
>
> oh man. It's so easy to do stuff in D ;-)
>
> But this leads me to a new problem now.
>
> When I run my code below, I get ints printed instead of
> doubles??
>
> ---------------------
> module test;
>
> import std.stdio : writeln;
> import std.traits : isArray;
> import std.array : array;
> import std.range : iota;
>
>
> void main()
> {
> int[] intArr = iota(1, 11).array(); // 1..10
> double[] doubleArr = iota(1.0, 11.0).array(); // 1.0..10.0
> char[] charArr = iota('a', '{').array(); // a..z
>
> printArray(intArr);
> printArray(doubleArr); // why is it printing ints instead
> of doubles??
> printArray(charArr);
> }
>
> void printArray(T)(const ref T[] a) if (isArray!(T[]))
> {
> foreach(t; a)
> writeln(t);
> }
>
> ---------------------------------
2 Things:
1. You can just use writeln to directly print Arrays. If you want
a specific format for the array you can use writef/writefln
2. By default, writeln will print [1, 2, 3] when your array
contains [1.0, 2.0, 3.0], since thats considered neater. You can
use writefln to address that. You can see this here:
https://run.dlang.io/is/bNxIsH
You can read more about format strings here:
https://dlang.org/phobos/std_format.html#format-string
More information about the Digitalmars-d-learn
mailing list