www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 4605] New: Wrong print of an int[string] aa

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4605

           Summary: Wrong print of an int[string] aa
           Product: D
           Version: D2
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: nobody puremagic.com
        ReportedBy: bearophile_hugs eml.cc



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]

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 09 2010
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4605




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

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 11 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4605


Andrej Mitrovic <andrej.mitrovich gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrej.mitrovich gmail.com



18:47:36 PDT ---

 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] -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Aug 29 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4605




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]

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 29 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4605




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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 22 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4605


SomeDude <lovelydear mailmetrash.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |lovelydear mailmetrash.com



PDT ---
writeln now produces the intended output on 2.059.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 21 2012
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4605


Kenji Hara <k.hara.pg gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED



Associative array formatting was improved in 2.055.
https://github.com/D-Programming-Language/phobos/pull/126

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 23 2012