D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 4605 - Wrong print of an int[string] aa
Summary: Wrong print of an int[string] aa
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: x86 Windows
: P2 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-08-09 08:47 UTC by bearophile_hugs
Modified: 2012-04-23 22:32 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2010-08-09 08:47:54 UTC
This D2 program, compiled with dmd 2.048b:


import std.stdio;
void main() {
    int[string] aa = ["10":10, "20":20];
    writeln(aa);
}


Prints (note the 1:10, that is a bug):
20:20 1:10


The expected less buggy and less barbaric output is:
["10": 10, "20": 20]
Or:
["20": 20, "10": 10]
Comment 1 bearophile_hugs 2010-08-11 14:52:18 UTC
This D2 code:

import std.stdio;
void main() {
    int[int[int]] aa;
    aa[[5:6, 7:8]] = 2;
    writeln(aa);
}


gives the useless output:
5:6 7:8:2
Comment 2 Andrej Mitrovic 2010-08-29 18:47:36 UTC
(In reply to comment #0)
> This D2 program, compiled with dmd 2.048b:
> 
> 
> import std.stdio;
> void main() {
>     int[string] aa = ["10":10, "20":20];
>     writeln(aa);
> }
> 
> 
> Prints (note the 1:10, that is a bug):
> 20:20 1:10
> 
> 
> The expected less buggy and less barbaric output is:
> ["10": 10, "20": 20]
> Or:
> ["20": 20, "10": 10]

It get's even worse:

import std.stdio;
void main() {
    int[string] aa = ["100":1, "200":1, "300":1, "400":1];
    writeln(aa);
}

Prints:

400:1 3:1 2:1 1:1

For your second code, I agree. I would prefer if D took an approach similar to other languages and printed out something like:

[[5:6, 7:8]:2]
Comment 3 bearophile_hugs 2010-08-29 19:06:06 UTC
Whitespace is important for readability, so instead of:
[[5:6, 7:8]:2]

It's better to print a space after the colon:
[[5: 6, 7: 8]: 2]
Comment 4 bearophile_hugs 2011-06-22 14:39:31 UTC
This function (related to the coding Kata Word Chains) creates an associative array where the keys are the start chars of words, and the values are sets of words. For simplicity in D2 I have implemented the string sets as bool[string].


import std.stdio, std.string;

bool[string][char] foo(string[] names) {
    typeof(return) result;
    foreach (name; names)
        result[name[0]][name] = true;
    return result;
}

auto names = "mary patricia linda barbara elizabeth jennifer
    maria susan margaret dorothy lisa nancy karen betty helen
    sandra donna carol ruth sharon michelle laura sarah
    kimberly deborah jessica shirley cynthia angela melissa
    brenda amy anna rebecca virginia kathleen pamela";

void main() {
    writeln(foo(names.split()));
}


The original complete program didn't have to print this result, but there I have created a bug, so I have had to print result, as I have done in this reduced program. This is the printout:


p:patricia:true pamela:true l:linda:true lisa:true laura:true d:dorothy:true donna:true deborah:true h:helen:true m:melissa:true margaret:true michelle:true maria:true mary:true e:elizabeth:true a:angela:true anna:true amy:true b:barbara:true betty:true brenda:true j:jessica:true jennifer:true n:nancy:true r:ruth:true rebecca:true v:virginia:true s:sharon:true susan:true shirley:true sandra:true sarah:true k:kathleen:true karen:true kimberly:true c:cynthia:true carol:true


For me this is very bad, I am not able to read it well. Nesting of dictionaries produces a hard to read output.

To better show what I mean this a Python2.6 translation (here I have used true sets, that are built-in, but the situation doesn't change a lot):

from collections import defaultdict

def foo(names):
    result = defaultdict(set)
    for name in names:
        result[name[0]].add(name)
    return result

names = """mary patricia linda barbara elizabeth jennifer
    maria susan margaret dorothy lisa nancy karen betty helen
    sandra donna carol ruth sharon michelle laura sarah
    kimberly deborah jessica shirley cynthia angela melissa
    brenda amy anna rebecca virginia kathleen pamela"""

print foo(names.split())


Its textual output allows me to tell apart sub-dictionaries:

defaultdict(<type 'set'>, {'a': set(['amy', 'anna', 'angela']), 'c': set(['carol', 'cynthia']), 'b': set(['barbara', 'betty', 'brenda']), 'e': set(['elizabeth']), 'd': set(['dorothy', 'donna', 'deborah']), 'h': set(['helen']), 'k': set(['kathleen', 'kimberly', 'karen']), 'j': set(['jessica', 'jennifer']), 'm': set(['margaret', 'melissa', 'michelle', 'mary', 'maria']), 'l': set(['laura', 'linda', 'lisa']), 'n': set(['nancy']), 'p': set(['pamela', 'patricia']), 's': set(['sarah', 'sharon', 'sandra', 'shirley', 'susan']), 'r': set(['ruth', 'rebecca']), 'v': set(['virginia'])})


Using pprint (pretty print) from the Python standard library it improves:
from pprint import pprint
pprint(dict(foo(names.split())))


{'a': set(['amy', 'angela', 'anna']),
 'b': set(['barbara', 'betty', 'brenda']),
 'c': set(['carol', 'cynthia']),
 'd': set(['deborah', 'donna', 'dorothy']),
 'e': set(['elizabeth']),
 'h': set(['helen']),
 'j': set(['jennifer', 'jessica']),
 'k': set(['karen', 'kathleen', 'kimberly']),
 'l': set(['laura', 'linda', 'lisa']),
 'm': set(['margaret', 'maria', 'mary', 'melissa', 'michelle']),
 'n': set(['nancy']),
 'p': set(['pamela', 'patricia']),
 'r': set(['rebecca', 'ruth']),
 's': set(['sandra', 'sarah', 'sharon', 'shirley', 'susan']),
 'v': set(['virginia'])}


This is even better:

{'a': {"amy", "angela", "anna"},
 'b': {"barbara", "betty", "brenda"},
 'c': {"carol", "cynthia"},
 'd': {"deborah", "donna", "dorothy"},
 'e': {"elizabeth"},
 'h': {"helen"},
 'j': {"jennifer", "jessica"},
 'k': {"karen", "kathleen", "kimberly"},
 'l': {"laura", "linda", "lisa"},
 'm': {"margaret", "maria", "mary", "melissa", "michelle"},
 'n': {"nancy"},
 'p': {"pamela", "patricia"},
 'r': {"rebecca", "ruth"},
 's': {"sandra", "sarah", "sharon", "shirley", "susan"},
 'v': {"virginia"}
}


If you want a more apples-to-apples comparison this is Python code that uses the same data structure used by the D code:

from collections import defaultdict

def foo(names):
    result = defaultdict(dict)
    for name in names:
        result[name[0]][name] = True
    return result

names = """mary patricia linda barbara elizabeth jennifer
    maria susan margaret dorothy lisa nancy karen betty helen
    sandra donna carol ruth sharon michelle laura sarah
    kimberly deborah jessica shirley cynthia angela melissa
    brenda amy anna rebecca virginia kathleen pamela"""

print foo(names.split())


Its textual output:

defaultdict(<type 'dict'>, {'a': {'amy': True, 'anna': True, 'angela': True}, 'c': {'carol': True, 'cynthia': True}, 'b': {'barbara': True, 'betty': True, 'brenda': True}, 'e': {'elizabeth': True}, 'd': {'dorothy': True, 'donna': True, 'deborah': True}, 'h': {'helen': True}, 'k': {'kathleen': True, 'kimberly': True, 'karen': True}, 'j': {'jessica': True, 'jennifer': True}, 'm': {'margaret': True, 'melissa': True, 'michelle': True, 'mary': True, 'maria': True}, 'l': {'laura': True, 'linda': True, 'lisa': True}, 'n': {'nancy': True}, 'p': {'pamela': True, 'patricia': True}, 's': {'sarah': True, 'sharon': True, 'sandra': True, 'shirley': True, 'susan': True}, 'r': {'ruth': True, 'rebecca': True}, 'v': {'virginia': True}})


Using pprint:

{'a': {'amy': True, 'angela': True, 'anna': True},
 'b': {'barbara': True, 'betty': True, 'brenda': True},
 'c': {'carol': True, 'cynthia': True},
 'd': {'deborah': True, 'donna': True, 'dorothy': True},
 'e': {'elizabeth': True},
 'h': {'helen': True},
 'j': {'jennifer': True, 'jessica': True},
 'k': {'karen': True, 'kathleen': True, 'kimberly': True},
 'l': {'laura': True, 'linda': True, 'lisa': True},
 'm': {'margaret': True,
       'maria': True,
       'mary': True,
       'melissa': True,
       'michelle': True},
 'n': {'nancy': True},
 'p': {'pamela': True, 'patricia': True},
 'r': {'rebecca': True, 'ruth': True},
 's': {'sandra': True,
       'sarah': True,
       'sharon': True,
       'shirley': True,
       'susan': True},
 'v': {'virginia': True}}


Even without pprint the printout of the default dict is usable for my debugging because it allows me to tell apart the sub-dictionaries. Another help comes from using "" and '' around chars and strings present inside collections.


A prettyPrint() function in Phobos will help, but first in D2 I'd like writeln() to print that D data structure more or less like this:

['a': ["amy": true, "anna": true, "angela": true], 'c': ["carol": true, "cynthia": true], 'b': ["barbara": true, "betty": true, "brenda": true], 'e': ["elizabeth": true], 'd': ["dorothy": true, "donna": true, "deborah": true], 'h': ["helen": true], 'k': ["kathleen": true, "kimberly": true, "karen": true], 'j': ["jessica": true, "jennifer": true], 'm': ["margaret": true, "melissa": true, "michelle": true, "mary": true, "maria": true], 'l': ["laura": true, "linda": true, "lisa": true], 'n': ["nancy": true], 'p': ["pamela": true, "patricia": true], 's': ["sarah": true, "sharon": true, "sandra": true, "shirley": true, "susan": true], 'r': ["ruth": true, "rebecca": true], 'v': ["virginia": true]]


This is allows me to use the printout for debugging, especially when I reduce the number of names for debugging purposes:

['a': ["amy": true, "anna": true], 'c': ["carol": true], 'b': ["barbara": true, "betty": true], 'd': ["dorothy": true], 'k': ["kathleen": true, "karen": true], 's': ["sandra": true, "shirley": true], 'v': ["virginia": true]]

---------------------

KennyTM~ suggests:

     writeln(to!string(foo(names.split())));

this gives

[p:[patricia:true, pamela:true], l:[linda:true, lisa:true, laura:true],
d:[dorothy:true, donna:true, deborah:true], h:[helen:true],
m:[melissa:true, margaret:true, michelle:true, maria:true, mary:true],
e:[elizabeth:true], a:[angela:true, anna:true, amy:true],
b:[barbara:true, betty:true, brenda:true], j:[jessica:true,
jennifer:true], n:[nancy:true], r:[ruth:true, rebecca:true],
v:[virginia:true], s:[sharon:true, susan:true, shirley:true,
sandra:true, sarah:true], k:[kathleen:true, karen:true, kimberly:true],
c:[cynthia:true, carol:true]]

This is better, but this isn't the default representation, I'd like a space after the colons, and ' " around chars and strings in collections.
Comment 5 SomeDude 2012-04-21 15:15:55 UTC
writeln now produces the intended output on 2.059.
Comment 6 Kenji Hara 2012-04-23 22:32:24 UTC
Associative array formatting was improved in 2.055.
https://github.com/D-Programming-Language/phobos/pull/126