www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - load data from txt file

reply aerto <investotxrt gmail.com> writes:
Hello and happy new year im new in d so i have a question

i have into a txt file named users.txt the bellow

["admin":"123456789"]
["test":"test345"]


im my app

string[string] data;

so i need to load users.txt content into data in order to be able 
to run


writeln(data["admin"]); // i want this to print 123456789
writeln(data["test"]); // i want this to print test345
Jan 02
next sibling parent reply visitor <visitor gmail.com> writes:
On Tuesday, 2 January 2018 at 22:08:52 UTC, aerto wrote:
 Hello and happy new year im new in d so i have a question

 writeln(data["admin"]); // i want this to print 123456789
 writeln(data["test"]); // i want this to print test345
if you can rewrite your users.txt file like so : ["admin":"123456789", "test":"test345"] with the help of import expression /-J command line switch (or dub stringImportPaths) https://dlang.org/spec/expression.html#import_expressions "dmd -release -J. yourfile.d" for example if users.txt is in same folder import std.stdio; string[string] data; static this() { // of course you could also do this in main() data = mixin(import("users.txt")); } void main(string[] args) { writefln("test => %s", data["test"]); }
Jan 02
parent Muld <2 2.2> writes:
On Tuesday, 2 January 2018 at 23:25:21 UTC, visitor wrote:
 On Tuesday, 2 January 2018 at 22:08:52 UTC, aerto wrote:
 Hello and happy new year im new in d so i have a question

 writeln(data["admin"]); // i want this to print 123456789
 writeln(data["test"]); // i want this to print test345
if you can rewrite your users.txt file like so : ["admin":"123456789", "test":"test345"] with the help of import expression /-J command line switch (or dub stringImportPaths) https://dlang.org/spec/expression.html#import_expressions "dmd -release -J. yourfile.d" for example if users.txt is in same folder import std.stdio; string[string] data; static this() { // of course you could also do this in main() data = mixin(import("users.txt")); } void main(string[] args) { writefln("test => %s", data["test"]); }
This means you need to recompile your code any time you want to change users.txt. Which would not be ideal from a user's perspective of the program.
Jan 02
prev sibling parent reply Tony <tonytdominguez aol.com> writes:
On Tuesday, 2 January 2018 at 22:08:52 UTC, aerto wrote:
 Hello and happy new year im new in d so i have a question

 i have into a txt file named users.txt the bellow

 ["admin":"123456789"]
 ["test":"test345"]


 im my app

 string[string] data;

 so i need to load users.txt content into data in order to be 
 able to run


 writeln(data["admin"]); // i want this to print 123456789
 writeln(data["test"]); // i want this to print test345
This seems to work (on Linux with no error checking) but is using the deprecated removechars() which gets deleted May 2018. There should be a simple fix using std.regex.replaceAll but I can't even get a successful compile right now (templates aren't deducing). Someone else should know what the correct replacement is for removechars(). import std.stdio; import std.string; import std.algorithm; void main() { string line; string[string] data; auto f = File("users.txt","r"); while ((line = f.readln('\n')) !is null) { string trimmed = removechars!string(line,"[\\[\\]\"\n\r]"); auto fields = findSplit(trimmed,":"); data[fields[0]] = fields[2]; } writeln(data); writeln("data for key admin:",data["admin"]); f.close(); }
Jan 02
parent reply codephantom <me noyb.com> writes:
On Wednesday, 3 January 2018 at 05:45:32 UTC, Tony wrote:
 Someone else should know what the correct replacement
 is for removechars().
the replacement is known as 'programming' ;-) //string trimmed = removechars!string(line,"[\\[\\]\"\n\r]"); string trimmed; foreach(c; line) { if(c != '[' && c != ']' && c != '\"' && c != '\r' && c != '\n' ) trimmed ~= c; }
Jan 03
parent reply Tony <tonytdominguez aol.com> writes:
On Thursday, 4 January 2018 at 05:52:35 UTC, codephantom wrote:
 On Wednesday, 3 January 2018 at 05:45:32 UTC, Tony wrote:
 Someone else should know what the correct replacement
 is for removechars().
the replacement is known as 'programming' ;-) //string trimmed = removechars!string(line,"[\\[\\]\"\n\r]"); string trimmed; foreach(c; line) { if(c != '[' && c != ']' && c != '\"' && c != '\r' && c != '\n' ) trimmed ~= c; }
OK, thanks. The removechars() note about deprecation said to use std.regex instead so I have been looking at that and after a struggle did make some use of std.regex.replaceAll. Reminded me of the famous Jamie Zawinski quote: "Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems. import std.stdio; import std.string; import std.algorithm; import std.regex; import std.file; import std.format : format; void checkLine(string line,long line_number) { // not expecting any whitespace or extra colons. Each line: // ["key":"value"] long colon_pos = std.string.indexOf(line,':'); assert(colon_pos != -1,format("ERROR: no colon on line %s",line_number)); long splitter_string_pos = std.string.indexOf(line,"\":\""); assert(splitter_string_pos != -1, format("ERROR: line %s missing quote(s) adjacent to :",line_number)); assert(line[0..2] == "[\"",format("ERROR: no [\" at line %s start",line_number)); assert(line[line.length - 2 .. line.length] == "\"]", format("ERROR: no \"] at end of line %s",line_number)); } void main() { string[string] data; string filename = "users.txt"; assert( std.file.exists(filename), format("ERROR: file %s not found",filename)); auto f = std.stdio.File("users.txt","r"); scope(exit) { f.close(); } string line = f.readln!(); long line_number = 0; while ( line !is null) { import std.uni : lineSep; line_number++; checkLine(std.string.chomp!(string)(line),line_number); auto fields = std.algorithm.findSplit(line,"\":\""); string key = std.regex.replaceAll(fields[0],regex(`^\["(.*)$`),"$1"); string value = std.regex.replaceAll(fields[2],regex(`^(.*)"\]\r?\n$`),"$1"); data[key] = value; line = f.readln!(); } writeln(data); writeln("value for key admin:",data["admin"]); writeln("value for key test:",data["test"]); }
Jan 04
parent codephantom <me noyb.com> writes:
On Thursday, 4 January 2018 at 08:39:04 UTC, Tony wrote:
 OK, thanks. The removechars() note about deprecation said to 
 use std.regex instead so I have been looking at that and after 
 a struggle did make some use of std.regex.replaceAll. Reminded 
 me of the famous Jamie Zawinski quote: "Some people, when 
 confronted with a problem, think "I know, I'll use regular 
 expressions." Now they have two problems.
If I see this in someones code - import std.regex; - it immediately causes me concerns - not just because I do not understand it, but there is no way for me to know whether that person understands it. So that is a well known quote..for a reason ;-) I've had a long career without regex, and I plan to keep it that way.
Jan 04