Re: Returning multiple values from a function

2017-09-04 Thread Azi Hassan via Digitalmars-d-learn

On Monday, 4 September 2017 at 09:22:25 UTC, Vino.B wrote:

Output :
1
2
["C:\\Temp\\TEAM1\\BACKUP", "C:\\Temp\\TEAM2\\ARCHIVE"]

Required Output:
Test1 = 1
Test2 = 2
Path = ["C:\\Temp\\TEAM1\\BACKUP", "C:\\Temp\\TEAM2\\ARCHIVE"]

From,
Vino.B


If you just need it to be displayed then you can add it to the 
writeln call : writeln("Test 1 = ", Params[0]);


But if you want to do it dynamically then maybe you should be 
using an associative array or a tuple of tuples :


import std.stdio;
import std.array;
import std.typecons;

alias Result = Tuple!(
Tuple!(string, int),
Tuple!(string, int),
Tuple!(string, string[])
);

Result Params () {
int Test1;
int Test2;
string[] File1;
string[] File2;
	auto Path = appender!(string[]); //string[] path; 
path.reserve(2) ?

Test1 = 1;
Test2 = 2;
File1 = ["C:\\Temp\\TEAM1\\BACKUP"];
File2 = ["C:\\Temp\\TEAM2\\ARCHIVE"];
Path ~= File1;
Path ~= File2;
return tuple(
tuple("Test1", Test1),
tuple("Test2", Test2),
tuple("Path", Path.data)
);

}

void main (){
auto res = Params();
writeln(res[0][0], " = ", res[0][1]);
writeln(res[1][0], " = ", res[1][1]);
writeln(res[2][0], " = ", res[2][1]);
}

You can also loop through res :

void main (){
auto res = Params();
foreach(r; res)
writeln(r[0], " = ", r[1]);
}


Re: Returning multiple values from a function

2017-09-04 Thread crimaniak via Digitalmars-d-learn

On Monday, 4 September 2017 at 09:22:25 UTC, Vino.B wrote:

 Thank you very much, i have used your idea and was able to 
resolve, and i need one more favor. the below code outputs the 
value but i need the name of the variable + value as below.


Output :
1
2
["C:\\Temp\\TEAM1\\BACKUP", "C:\\Temp\\TEAM2\\ARCHIVE"]

Required Output:
Test1 = 1
Test2 = 2
Path = ["C:\\Temp\\TEAM1\\BACKUP", "C:\\Temp\\TEAM2\\ARCHIVE"]


For fixed names case you can hardcode it:
writeln("Test1 = ", Params[0]);
writeln("Test2 = ", Params[1]);
writeln("Path = ",  Params[2]);

You can't print the actual name of the variable used in tuple 
constructing because tuple doesn't store it. More of this, the 
tuple can be constructed from expression without a name, so it's 
impossible in common case.


Re: Returning multiple values from a function

2017-09-04 Thread Vino.B via Digitalmars-d-learn

On Monday, 4 September 2017 at 07:40:23 UTC, crimaniak wrote:

On Monday, 4 September 2017 at 07:27:12 UTC, Vino.B wrote:

Hi,

 Can you help me in how to return multiple values from a 
function, the below code is throwing an error as below


import std.stdio: writeln;
import std.typecons: tuple, Tuple;

Tuple!(int, string[]) Params () {
return tuple(1, ["C:\\Temp\\TEAM1\\BACKUP", 
"C:\\Temp\\TEAM2\\ARCHIVE"]);

}

void main (){
Params.writeln;
}


Hi,

 Thank you very much, i have used your idea and was able to 
resolve, and i need one more favor. the below code outputs the 
value but i need the name of the variable + value as below.


Output :
1
2
["C:\\Temp\\TEAM1\\BACKUP", "C:\\Temp\\TEAM2\\ARCHIVE"]

Required Output:
Test1 = 1
Test2 = 2
Path = ["C:\\Temp\\TEAM1\\BACKUP", "C:\\Temp\\TEAM2\\ARCHIVE"]

Program:
import std.stdio: writeln;
import std.typecons: tuple, Tuple;
import std.array: appender;

Tuple!(int,int, string[]) Params () {
int Test1;
int Test2;
string[] File1;
string[] File2;
auto Path = appender!(string[]);
Test1 = 1;
Test2 = 2;
File1 = ["C:\\Temp\\TEAM1\\BACKUP"];
File2 = ["C:\\Temp\\TEAM2\\ARCHIVE"];
Path ~= File1;
Path ~= File2;
return tuple (Test1, Test2, Path.data);

}

void main (){
writeln(Params[0]);
writeln(Params[1]);
writeln(Params[2]);
}
return tuple (Test1, Test1Test2, Path.data);

}

void main (){
writeln(Params[0]);
writeln(Params[1]);
}

From,
Vino.B


Re: Returning multiple values from a function

2017-09-04 Thread crimaniak via Digitalmars-d-learn

On Monday, 4 September 2017 at 07:27:12 UTC, Vino.B wrote:

Hi,

 Can you help me in how to return multiple values from a 
function, the below code is throwing an error as below


import std.stdio: writeln;
import std.typecons: tuple, Tuple;

Tuple!(int, string[]) Params () {
return tuple(1, ["C:\\Temp\\TEAM1\\BACKUP", 
"C:\\Temp\\TEAM2\\ARCHIVE"]);

}

void main (){
Params.writeln;
}


Returning multiple values from a function

2017-09-04 Thread Vino.B via Digitalmars-d-learn

Hi,

 Can you help me in how to return multiple values from a 
function, the below code is throwing an error as below


Program:
import std.stdio: writeln;
import std.typecons: tuple, Tuple;

Tuple!(int, string[]) Params () {
int Test1;
string[] Path;
Test1 = 1;
Path = ["C:\\Temp\\TEAM1\\BACKUP", "C:\\Temp\\TEAM2\\ARCHIVE"];
return Test1;
return Path;
}

void main (){
int Test1;
string[] Path;
Params;
writeln(Test1);
writeln(Path);
}

Error:
TEx1.d(9): Error: cannot implicitly convert expression Test1 of 
type int to Tuple!(int, string[])
TEx1.d(10): Error: cannot implicitly convert expression Path of 
type string[] to Tuple!(int, string[])


From,
Vino.B