Speak now
Please Wait Image Converting Into Text...
Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Challenge yourself and boost your learning! Start the quiz now to earn credits.
Unlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
General Tech Learning Aids/Tools 2 years ago
Posted on 16 Aug 2022, this text provides information on Learning Aids/Tools related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.
Turn Your Knowledge into Earnings.
I'm trying to learn D but am struggling with lack of documentation (or my understanding of it), so I came here. I already asked a different but unrelated question earlier today.
Anyway, here goes:
I would like to parse a string for different things.
String format is something like:
[] [parameters]
If there is no label, there is mandatory whitespace. Parameters can be comma-delimited. Parameter types are dependent on the mnemonic.
I would like to use std.conv: parse from the Phobos library to aid me, but I fail to understand the documentation on how to parse a "word", as in, some characters seperated by whitespace on either end. It works fine for integers and the like as int i = parse!int(line). But if I were to do string s = parse!string(line) it would grab the entire line.
std.conv: parse
int i = parse!int(line)
string s = parse!string(line)
I cold parse this by hand, using char** (or, ref string) as a datatype, just like I did when I wrote this in C. But I'm learning D to not have to.
char**
ref string
I tried something like this to do it manually:
string get_word(ref string s) { int i = 0; while (i < s.length && isAlphaNum(s[i])) i++; string word = s[0 .. i]; s = s[i+1 .. $]; return word; }
Is this a good way to do it? Is there a cleaner way? A faster way? A safer way, perhaps? I'm not sure the i+1 index always exists.
i+1
Thanks for the help!
My faith in D is slightly dwindling already, as I've run into all sorts of problems. But the path is surely going to be worth it.
code is wrote on the fly
import std.string; import std.stdio; import std.algorithm; import std.math; enum string[] separators = [ " ", "\t", ",", ";", "\n", "\r\n" ]; string get_word( ref string s ){ string token; sizediff_t storePositions[separators.length + 1]; // set size array to the number of separator in array "separators" and latest field for current string lenght foreach( i, separator; separators ){ // compute position for each separator sizediff_t position = countUntil( s, separator ); if( position == -1 ) position = sizediff_t.max; storePositions[i] = position; } storePositions[ $ -1 ] = s.length; sizediff_t end = reduce!min( storePositions ); token = s[0 .. end].idup; writefln( "%s | %d", s, end ); return token; } void main( string[] args ){ string s = "a long;string\tyeah\n strange; ok"; bool isRunning= true; size_t start = 0; writefln( "parse: %s", s ); while( isRunning ){ string result = get_word( s[ start .. $] ); if( result == "" ) isRunning = false; else{ start += result.length + 1; result = get_word( s[ start .. $] ); } writefln( "token: %s, position: %d", result, start ); writeln( "----" ); } }
output:
parse: a long;string yeah strange; ok a long;string yeah strange; ok | 1 long;string yeah strange; ok | 4 token: long, position: 2 ---- long;string yeah strange; ok | 4 string yeah strange; ok | 6 token: string, position: 7 ---- string yeah strange; ok | 6 yeah strange; ok | 4 token: yeah, position: 14 ---- yeah strange; ok | 4 strange; ok | 0 token: , position: 19 ---- strange; ok | 0 token: , position: 19
No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.
General Tech 9 Answers
General Tech 7 Answers
General Tech 3 Answers
General Tech 2 Answers
Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.