KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I'm new to D programming language, just started reading The D Programming Language book. I run into error when trying one associative array example code #!/usr/bin/rdmd import std.stdio, std.string; void main() { uint[string] dict; foreach (line; stdin.byLine()) { foreach (word; splitter(strip(line))) { if (word in dict) continue; auto newId = dict.length; dict[word] = newId; writeln(newId, '\t', word); } } } DMD shows this Error message: ./vocab.d(11): Error: associative arrays can only be assigned values with immutable keys, not char[] I'm using DMD compile 2.051 I was guessing the rules for associative arrays has changed since the TDPL book. How should I use Associative arrays with string keys? Thanks. Update: I found the solution in later parts of the book. use string.idup to make a duplicate immutable value before putting into the array. so dict[word.idup] = newId; would do the job. But is that efficient ?
Tags (comma-separated)
Save Edits
Cancel