digitalmars.D.learn - Array of Associative arrays
- jicman (30/30) Jul 27 2005 Greetings!
- Chris Sauls (20/45) Jul 27 2005 Not surprising. Look at these two lines:
- jicman (4/49) Jul 27 2005 Yep. You didn't misunderstand something.
- Chris Sauls (5/7) Jul 27 2005 Okay good... I just happened to have an inspired moment or I don't think...
- jicman (2/9) Jul 27 2005
- novice2 (3/6) Jul 28 2005 this is example, why some D programmers like this:
- Chris Sauls (6/13) Jul 29 2005 I've done this in some programs myself. I'm personally fond of:
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é
Jul 27 2005
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:
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"]):
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
Jul 27 2005
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 charNot surprising. Look at these two lines: 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"]): 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
Jul 27 2005
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
Jul 27 2005
Chris Sauls says...jicman wrote:Thank God for inspired moments. :-)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
Jul 27 2005
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*/
Jul 28 2005
novice2 wrote:I've done this in some programs myself. I'm personally fond of: -- Chris SaulsMaybe 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*/
Jul 29 2005









jicman <jicman_member pathlink.com> 