Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
July 27, 2005 Array of Associative arrays | ||||
---|---|---|---|---|
| ||||
Greetings! Please help me understand this one: import std.stdio; import std.string; char[][char[]] ProcessUserLoginEntry(char[][char[]] ul,char[] line) { char[][] MyS0 = std.string.split(line," >>> Login: User "); char[][] MyS1 = std.string.split(MyS0[1]," logged in"); char[] user = MyS1[0]; MyS1 = std.string.split(MyS0[0]," STATUS "); ul[user].length = ul[user].length + 1; ul[user][ul[user].length - 1] = MyS1[1]; return ul; } void main() { char[] s = "20020729154320 STATUS Jul 29, 2002 3:43:20 PM >>>"; s ~= " Login: User blah logged in "; char[][char[]] aa; aa = ProcessUserLoginEntry(aa, s); } when I compile it, I get, jic 15:33:58-> build AssArrays.d AssArrays.d(10): cannot implicitly convert expression (MyS1[1]) of type char[] to char well, MyS1 is an array of char[][], so MyS1[1] should be char[]. Right? Any help would be greatly appreciate it. I mean, I know how to go about doing this another way, but this is the easiest for me. thanks, josé |
July 27, 2005 Re: Array of Associative arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to jicman | jicman wrote: > import std.stdio; > import std.string; > char[][char[]] ProcessUserLoginEntry(char[][char[]] ul,char[] line) > { > char[][] MyS0 = std.string.split(line," >>> Login: User "); > char[][] MyS1 = std.string.split(MyS0[1]," logged in"); > char[] user = MyS1[0]; > MyS1 = std.string.split(MyS0[0]," STATUS "); > ul[user].length = ul[user].length + 1; > ul[user][ul[user].length - 1] = MyS1[1]; > return ul; > } > void main() > { > char[] s = "20020729154320 STATUS Jul 29, 2002 3:43:20 PM >>>"; s ~= " Login: User blah logged in "; > char[][char[]] aa; > aa = ProcessUserLoginEntry(aa, s); > } > > when I compile it, I get, > > jic 15:33:58-> build AssArrays.d AssArrays.d(10): cannot implicitly convert expression (MyS1[1]) of type char[] > to char Not surprising. Look at these two lines: # char[][char[]] ProcessUserLoginEntry(char[][char[]] ul,char[] line) # ul[user][ul[user].length - 1] = MyS1[1]; In the first one you declare 'ul' to be type 'char[][char[]]' or, reading right-to-left as per D convention: an associative array, keyed to arrays of char, of arrays of char. Or, a map of strings to strings. Then MyS1 is of t ype 'char[][]' or: an array of arrays of char. Or, an array of strings. The problem: the expression 'ul[user][ul[user].length - 1]' evaluates to a char. Look at it this way (assume ul[user] == "fred", and MyS1 contains ["foo", "bar"]): ## (ul[user])[((ul[user]).length) - 1] = MyS1[1]; ## (ul[user])[("fred".length) - 1] = MyS1[1]; ## "fred"[("fred".length) - 1] = MyS1[1]; ## "fred"[4 - 1] = MyS1[1]; ## "fred"[3] = MyS1[1]; ## 'd' = "bar"; Maybe the type you were wanting was 'char[][][char[]]' meaning: an associative array, keyed to arrays of char, of arrays, of arrays of char. Or, a map of strings to arrays of strings. Either that or I'm misunderstanding something. -- Chris Sauls |
July 27, 2005 Re: Array of Associative arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chris Sauls |
Yep. You didn't misunderstand something.
thanks.
josé
Chris Sauls says...
>
>jicman wrote:
>> import std.stdio;
>> import std.string;
>> char[][char[]] ProcessUserLoginEntry(char[][char[]] ul,char[] line)
>> {
>> char[][] MyS0 = std.string.split(line," >>> Login: User ");
>> char[][] MyS1 = std.string.split(MyS0[1]," logged in");
>> char[] user = MyS1[0];
>> MyS1 = std.string.split(MyS0[0]," STATUS ");
>> ul[user].length = ul[user].length + 1;
>> ul[user][ul[user].length - 1] = MyS1[1];
>> return ul;
>> }
>> void main()
>> {
>> char[] s = "20020729154320 STATUS Jul 29, 2002 3:43:20 PM >>>";
>> s ~= " Login: User blah logged in ";
>> char[][char[]] aa;
>> aa = ProcessUserLoginEntry(aa, s);
>> }
>>
>> when I compile it, I get,
>>
>> jic 15:33:58-> build AssArrays.d
>> AssArrays.d(10): cannot implicitly convert expression (MyS1[1]) of type char[]
>> to char
>
>Not surprising. Look at these two lines:
># char[][char[]] ProcessUserLoginEntry(char[][char[]] ul,char[] line)
># ul[user][ul[user].length - 1] = MyS1[1];
>
>In the first one you declare 'ul' to be type 'char[][char[]]' or, reading right-to-left as per D convention: an associative array, keyed to arrays of char, of arrays of char. Or, a map of strings to strings. Then MyS1 is of t ype 'char[][]' or: an array of arrays of char. Or, an array of strings.
>
>The problem: the expression 'ul[user][ul[user].length - 1]' evaluates to a char. Look at
>it this way (assume ul[user] == "fred", and MyS1 contains ["foo", "bar"]):
>## (ul[user])[((ul[user]).length) - 1] = MyS1[1];
>## (ul[user])[("fred".length) - 1] = MyS1[1];
>## "fred"[("fred".length) - 1] = MyS1[1];
>## "fred"[4 - 1] = MyS1[1];
>## "fred"[3] = MyS1[1];
>## 'd' = "bar";
>
>Maybe the type you were wanting was 'char[][][char[]]' meaning: an associative array, keyed to arrays of char, of arrays, of arrays of char. Or, a map of strings to arrays of strings. Either that or I'm misunderstanding something.
>
>-- Chris Sauls
|
July 27, 2005 Re: Array of Associative arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to jicman | jicman wrote: > Yep. You didn't misunderstand something. Okay good... I just happened to have an inspired moment or I don't think I would've seen it either. Hooray for stepping through expressions. > thanks. Anytime. -- Chris Sauls |
July 28, 2005 Re: Array of Associative arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chris Sauls | Chris Sauls says... > >jicman wrote: >> Yep. You didn't misunderstand something. > >Okay good... I just happened to have an inspired moment or I don't think I would've seen it either. Hooray for stepping through expressions. Thank God for inspired moments. :-) > >> thanks. > >Anytime. > >-- Chris Sauls |
July 29, 2005 Re: Array of Associative arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chris Sauls | >Maybe the type you were wanting was 'char[][][char[]]' meaning: an associative array, keyed to arrays of char, of arrays, of arrays of char. Or, a map of strings to arrays of strings. Either that or I'm misunderstanding something.
this is example, why some D programmers like this:
alias char[] string;
string[][string] a; /*easy to understand*/
|
July 29, 2005 Re: Array of Associative arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to novice2 | novice2 wrote:
>>Maybe the type you were wanting was 'char[][][char[]]' meaning: an associative array, keyed to arrays of char, of arrays, of arrays of char. Or, a map of strings to arrays of strings. Either that or I'm misunderstanding something.
> this is example, why some D programmers like this:
>
> alias char[] string;
> string[][string] a; /*easy to understand*/
I've done this in some programs myself. I'm personally fond of:
# alias char[] str8 ;
# alias wchar[] str16 ;
# alias dchar[] str32 ;
-- Chris Sauls
|
Copyright © 1999-2021 by the D Language Foundation