Re: Separate IP parts

2016-12-11 Thread Era Scarecrow via Digitalmars-d-learn

On Saturday, 10 December 2016 at 03:51:34 UTC, brocolis wrote:

How do I separate IP parts with dlang?

I found this very cool trick, with C++: 
http://stackoverflow.com/a/5328190


 Heh, I'd prefer to use sscanf vs using the streams.


Re: Separate IP parts

2016-12-11 Thread notna via Digitalmars-d-learn
On Saturday, 10 December 2016 at 13:25:13 UTC, Nicholas Wilson 
wrote:

On Saturday, 10 December 2016 at 13:21:40 UTC, notna wrote:

Those statements need to be inside a function.


Feel free to post a working example or, even better, a pull 
request with one ;)


Re: Separate IP parts

2016-12-10 Thread aberba via Digitalmars-d-learn
On Saturday, 10 December 2016 at 13:25:13 UTC, Nicholas Wilson 
wrote:

On Saturday, 10 December 2016 at 13:21:40 UTC, notna wrote:

On Saturday, 10 December 2016 at 08:03:00 UTC, biozic wrote:

[...]


Well, you know, that's one of the not so great things about 
Dlang... you cannot even trust the provided examples, if there 
are any:


[...]


Those statements need to be inside a function.


I guess that's the reputation complaints here a building up :)


Re: Separate IP parts

2016-12-10 Thread Nicholas Wilson via Digitalmars-d-learn

On Saturday, 10 December 2016 at 13:21:40 UTC, notna wrote:

On Saturday, 10 December 2016 at 08:03:00 UTC, biozic wrote:

[...]


Well, you know, that's one of the not so great things about 
Dlang... you cannot even trust the provided examples, if there 
are any:


[...]


Those statements need to be inside a function.


Re: Separate IP parts

2016-12-10 Thread notna via Digitalmars-d-learn

On Saturday, 10 December 2016 at 08:03:00 UTC, biozic wrote:

This would do the same. I wouldn't say it's a trick.

import std.format : formattedRead;
import std.stdio : writefln;

string ipAddr = "192.168.1.54";
int a, b, c, d;
formattedRead(ipAddr, "%d.%d.%d.%d", , , , );
writefln("%s %s %s %s", a, b, c, d);


Well, you know, that's one of the not so great things about 
Dlang... you cannot even trust the provided examples, if there 
are any:


C:\Temp\D>more formattedReadIps.d
import std.format;
string s = "hello!124:34.5";
string a;
int b;
double c;
formattedRead(s, "%s!%s:%s", , , );
assert(a == "hello" && b == 124 && c == 34.5);

C:\Temp\D>dmd -v formattedReadIps.d
binaryC:\D\dmd2\windows\bin\dmd.exe
version   v2.072.1
configC:\D\dmd2\windows\bin\sc.ini
parse formattedReadIps
formattedReadIps.d(6): Error: unexpected ( in declarator
formattedReadIps.d(6): Error: basic type expected, not "%s!%s:%s"
formattedReadIps.d(6): Error: found '"%s!%s:%s"' when expecting 
')'
formattedReadIps.d(6): Error: no identifier for declarator 
formattedRead(s, _error_)
formattedReadIps.d(6): Error: semicolon expected following 
function declaration

formattedReadIps.d(6): Error: declaration expected, not ','
formattedReadIps.d(7): Error: declaration expected, not 'assert'




Re: Separate IP parts

2016-12-10 Thread biozic via Digitalmars-d-learn

On Saturday, 10 December 2016 at 03:51:34 UTC, brocolis wrote:

How do I separate IP parts with dlang?

I found this very cool trick, with C++: 
http://stackoverflow.com/a/5328190


std::string ip ="192.168.1.54";
std::stringstream s(ip);
int a,b,c,d; //to store the 4 ints
char ch; //to temporarily store the '.'
s >> a >> ch >> b >> ch >> c >> ch >> d;
std::cout << a << "  " << b << "  " << c << "  "<< d;

I wonder what's the equivalent D code.


This would do the same. I wouldn't say it's a trick.

import std.format : formattedRead;
import std.stdio : writefln;

string ipAddr = "192.168.1.54";
int a, b, c, d;
formattedRead(ipAddr, "%d.%d.%d.%d", , , , );
writefln("%s %s %s %s", a, b, c, d);



Re: Separate IP parts

2016-12-09 Thread Anonymouse via Digitalmars-d-learn

On Saturday, 10 December 2016 at 03:51:34 UTC, brocolis wrote:

How do I separate IP parts with dlang?

I found this very cool trick, with C++: 
http://stackoverflow.com/a/5328190


std::string ip ="192.168.1.54";
std::stringstream s(ip);
int a,b,c,d; //to store the 4 ints
char ch; //to temporarily store the '.'
s >> a >> ch >> b >> ch >> c >> ch >> d;
std::cout << a << "  " << b << "  " << c << "  "<< d;

I wonder what's the equivalent D code.


Not much of a trick, but:

import std.algorithm : splitter, map;
import std.range : take;
import std.conv  : to;
import std.array : array;

string ip = "192.168.1.54";

auto parts = ip
.splitter('.')
.take(4)
.map!((a) => a.to!int)
.array;

assert(parts[0] == 192);
assert(parts[1] == 168);
assert(parts[2] == 1);
assert(parts[3] == 54);

Remove the .array to keep it lazy, but then you can't index it 
for the values, only walk through them in a foreach.


Separate IP parts

2016-12-09 Thread brocolis via Digitalmars-d-learn

How do I separate IP parts with dlang?

I found this very cool trick, with C++: 
http://stackoverflow.com/a/5328190


std::string ip ="192.168.1.54";
std::stringstream s(ip);
int a,b,c,d; //to store the 4 ints
char ch; //to temporarily store the '.'
s >> a >> ch >> b >> ch >> c >> ch >> d;
std::cout << a << "  " << b << "  " << c << "  "<< d;

I wonder what's the equivalent D code.