Re: "string" data type with readln

2022-07-25 Thread rikki cattermole via Digitalmars-d-learn



The version of readln you are using is[0]. This works by taking in a 
buffer of memory to write out, and returns how many codepoints were stored.


Because you are not reusing memory, not using this form you can of 
course use string[1] instead, rather than ``char[]``.


```d
string s = readln();
```

The definition of string is[2]:

```d
alias string  = immutable(char)[];
```

Note the immutable there, which means that each value in the slice 
cannot be modified. Hence why it can't be used as a buffer.


[0] https://dlang.org/phobos/std_stdio.html#.readln.2
[1] https://dlang.org/phobos/std_stdio.html#.readln
[2] https://github.com/dlang/dmd/blob/master/druntime/src/object.d#L69


Re: "string" data type with readln

2022-07-25 Thread Adam D Ruppe via Digitalmars-d-learn

On Monday, 25 July 2022 at 19:55:40 UTC, pascal111 wrote:
I tried to type small program, and tried to use "string" data 
type with "readln", but the compiler refused it, and I had to 
use "char[]".


the overload you used modifies the array you give it

try

string s = readln();



"string" data type with readln

2022-07-25 Thread pascal111 via Digitalmars-d-learn
I tried to type small program, and tried to use "string" data 
type with "readln", but the compiler refused it, and I had to use 
"char[]". So, if we can't use "string" with "readln", so what's 
its benefit? why we need to use it?


Code source:
https://github.com/pascal111-fra/D/blob/main/proj01.d


Re: string to type

2021-02-24 Thread Виталий Фадеев via Digitalmars-d-learn
On Thursday, 25 February 2021 at 06:53:28 UTC, Виталий Фадеев 
wrote:
On Thursday, 25 February 2021 at 06:51:11 UTC, Виталий Фадеев 
wrote:

Say, please,
how to convert string to type ?

[...]


Simple version of this question is:
   string t = "X";
   mixin t!();   // <-- HERE TROUBLE


I was stupid.
Solution is:
mixin ( "mixin " ~ IFACE.stringof[ 1 .. $ ] ~ "!();" );

Thanks!


string to type

2021-02-24 Thread Виталий Фадеев via Digitalmars-d-learn

Say, please,
how to convert string to type ?

Is:
interface IX
{
//
}

mixin template X()
{
//
}

// Mixin templates to class.
//   Scan class interfaces, then mix
mixin template Members()
{
alias T = typeof( this );

static
foreach ( IFACE; InterfacesTuple! T )
{
pragma( msg, IFACE );
pragma( msg, IFACE.stringof[ 1 .. $ ] );

static
if ( IFACE.stringof.length > 1 && IFACE.stringof[ 1 
.. $ ] )

{
mixin ( IFACE.stringof[ 1 .. $ ] )!(); // <-- 
HERE TROUBLE

}
}
}

class A : IX
{
mixin Members!();
}

Want:
// Mixin template
mixin IFACE.stringof[ 1 .. $ ] !();

Got:
Compilation error: "declaration expected"

source: https://run.dlang.io/is/smFaWc

Help wanted.



Re: string to type

2021-02-24 Thread Виталий Фадеев via Digitalmars-d-learn
On Thursday, 25 February 2021 at 06:51:11 UTC, Виталий Фадеев 
wrote:

Say, please,
how to convert string to type ?

[...]


Simple version of this question is:
   string t = "X";
   mixin t!();   // <-- HERE TROUBLE


Unexpected ' ' when converting from type string to type int

2015-12-29 Thread Michael S via Digitalmars-d-learn
Hello,  thanks for stopping in. I am fuddling through some 
exercises on a certain website, and I have come across a very 
frustrating bug I can't seem to fix.


The Problem:
Given a square matrix of size N×N, calculate the absolute 
difference between the sums of its diagonals.


Sample Input:
3
11 2 4
4 5 6
10 8 -12

First line is N, and the expected result of this example is 15.
If I hardcode in the value for N, this code works just peachy. 
It's when I am trying to actually read in the value of the first 
line from stdin that I am having a problem.


I seem to be unable to convert the string input to an integer to 
use later on. The code:


import std.stdio, std.math, std.string, std.conv;

void main(){
auto matrix_size = readln;

//below I have commented out the offending call to to!int and 
replaced it with a hardcoded value 3 in order to force it to work.

auto ms = 3;//matrix_size.to!int;
int[][] matrix = new int[][ms];
for(int i = 0; i < ms; i++){
string[] string_nums = readln.split;
foreach (num; string_nums){
auto value = num.to!int;
matrix[i] ~= value;
}
}

int primary_sum = 0;
int secondary_sum = 0;
int j = 0;
int i = ms - 1;
//determine diagonal sums
for(int row = 0; row < ms; ++row){
primary_sum += matrix[row][j];
secondary_sum += matrix[row][i];
++j;
--i;
}
auto result = abs(primary_sum - secondary_sum);
result.writeln;
}


Re: Unexpected ' ' when converting from type string to type int

2015-12-29 Thread Adam D. Ruppe via Digitalmars-d-learn

On Wednesday, 30 December 2015 at 01:36:56 UTC, Michael S wrote:

auto matrix_size = readln;


Change that to

auto matrix_size = readln.strip;


and you should be in business. readln() returns any leading 
spaces and the newline character at the end of the line too, 
which is why to is throwing.


The .strip function will trim that whitespace off.


Re: Unexpected ' ' when converting from type string to type int

2015-12-29 Thread Michael S via Digitalmars-d-learn
On Wednesday, 30 December 2015 at 01:38:32 UTC, Adam D. Ruppe 
wrote:

On Wednesday, 30 December 2015 at 01:36:56 UTC, Michael S wrote:

auto matrix_size = readln;


Change that to

auto matrix_size = readln.strip;


and you should be in business. readln() returns any leading 
spaces and the newline character at the end of the line too, 
which is why to is throwing.


The .strip function will trim that whitespace off.


Wow thank you Adam. I didn't expect to have a reply so quickly, 
especially a slam dunk like that one. It works perfectly now!