www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - why there is a [] at the end of assocArray

reply michaelbi <shunjie.bi gmail.com> writes:
input:
00100
11110
10110
10111
10101
01111
00111
11100
10000
11001
00010
01010

     code:
     void main()
     {
     	foreach(line; 
File("input.txt").byLine.map!(a=>a.idup).array.transposed){
     		auto sortgroup = line.array.strip.sort.group.assocArray;	
     		writeln(sortgroup);
     	}
     }

output:
['1':7, '0':5]
['1':5, '0':7]
['1':8, '0':4]
['1':7, '0':5]
['1':5, '0':7]
[]

so why there is a [] at the end of assocArray printed? thanks.
Jan 19 2022
parent reply Stanislav Blinov <stanislav.blinov gmail.com> writes:
On Wednesday, 19 January 2022 at 13:15:35 UTC, michaelbi wrote:

     	foreach(line; > 
 File("input.txt").byLine.map!(a=>a.idup).array.transposed)
 so why there is a [] at the end of assocArray printed? thanks.
...because there's an empty line at the end of input.txt?
Jan 19 2022
parent reply michaelbi <shunjie.bi gmail.com> writes:
On Wednesday, 19 January 2022 at 13:21:32 UTC, Stanislav Blinov 
wrote:
 On Wednesday, 19 January 2022 at 13:15:35 UTC, michaelbi wrote:

     	foreach(line; > 
 File("input.txt").byLine.map!(a=>a.idup).array.transposed)
 so why there is a [] at the end of assocArray printed? thanks.
...because there's an empty line at the end of input.txt?
i got it, though i still don't know where the [] come from. i just add strip here: a=>a.idup.strip
Jan 19 2022
next sibling parent jfondren <julian.fondren gmail.com> writes:
On Wednesday, 19 January 2022 at 14:06:45 UTC, michaelbi wrote:
 i got it, though i still don't know where the [] come from.
$ rdmd --eval 'writeln("".array.strip.sort.group.assocArray)' [] $ rdmd --eval 'writeln(typeid("".array.strip.sort.group.assocArray))' uint[dchar] It's what an empty AA looks like.
Jan 19 2022
prev sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 1/19/22 06:06, michaelbi wrote:
 On Wednesday, 19 January 2022 at 13:21:32 UTC, Stanislav Blinov wrote:
 On Wednesday, 19 January 2022 at 13:15:35 UTC, michaelbi wrote:

         foreach(line; > 
 File("input.txt").byLine.map!(a=>a.idup).array.transposed)
 so why there is a [] at the end of assocArray printed? thanks.
...because there's an empty line at the end of input.txt?
i got it, though i still don't know where the [] come from. i just add strip here: a=>a.idup.strip
Works for me on Linux. Perhaps there is an issue with Windows line endings? In any case, the .strip above would not be eliminating empty lines; you need to filter them out e.g. with byLine.filter!(line => !line.empty) Aside: Instead of copying the lines with .idup explicitly, there is .byLineCopy that already does that. Ali
Jan 19 2022
parent MichaelBi <shunjie.bi gmail.com> writes:
On Wednesday, 19 January 2022 at 16:36:36 UTC, Ali Çehreli wrote:
 On 1/19/22 06:06, michaelbi wrote:
 On Wednesday, 19 January 2022 at 13:21:32 UTC, Stanislav 
 Blinov wrote:
 On Wednesday, 19 January 2022 at 13:15:35 UTC, michaelbi 
 wrote:

 [...]
 [...]
...because there's an empty line at the end of input.txt?
i got it, though i still don't know where the [] come from. i just add strip here: a=>a.idup.strip
Works for me on Linux. Perhaps there is an issue with Windows line endings? In any case, the .strip above would not be eliminating empty lines; you need to filter them out e.g. with byLine.filter!(line => !line.empty) Aside: Instead of copying the lines with .idup explicitly, there is .byLineCopy that already does that. Ali
I am using windows. Thanks a lot for introducing those funcs. Now I am feeling the D’s quite interesting and powerful :)
Jan 19 2022