digitalmars.D - proposal: operator for push back on array?
- clayasaurus (21/21) Aug 20 2004 Hello. I know we already have ~= for character arrays and int arrays.
- Ben Hinkle (8/36) Aug 20 2004 Can you post an example? I just tried
- clayasaurus (8/25) Aug 20 2004 Aha. Well what I'd like is just to do
- Helmut Leitner (9/37) Aug 20 2004 item[0], item[1] refernce the first and second character.
- Vathix (15/18) Aug 20 2004 But ~= works for all arrays.
- clayasaurus (29/59) Aug 20 2004 Maybe I am just dumb or something. But anyway
- Ivan Senji (8/61) Aug 21 2004 To do what you want to do write:
- clayasaurus (9/92) Aug 21 2004 I guess my confusion arouse out of thinking
- Matthew (3/95) Aug 21 2004 Quite understandable. I fail to see why it should not.
- Charles Hixson (11/28) Aug 23 2004 ...
- Matthew (4/25) Aug 20 2004 I'm more than a little surprised that ~= does not work for arrays of all...
- clayasaurus (11/14) Aug 20 2004 What I would like (like push back in c++ vector) is for it to add one to...
- Andy Friesen (14/43) Aug 20 2004 The trick is that ~= appends a single element to an array, whereas ~
- Arcane Jill (19/30) Aug 20 2004 Is that really true? Wow. I had assumed that
- Andy Friesen (14/30) Aug 21 2004 In any setting where an array can contain an element of its own type,
- Ilya Minkov (3/12) Aug 23 2004 Agree and vote!
Hello. I know we already have ~= for character arrays and int arrays. However they don't work for class/struct arrays and character arrays of arrays. Right now, If I want to add something to a class or array of character arrays i have do to this... // this is what I do items.length = items.length+1; // first I add one to the array items[length-1] = "string"; // then I set the last index to what I want What I'd like to see is an operator that works like push_back() in c++ vectors. Where all you'd have to type is items &&= "string"; // i think &= is already used? or items pushback "string"; or or something to that effect, i'm not sure about what operator would fit nicely. What do you think? Or should we just leave this up to templates (DTL?) with a items.pushback("string"); syntax? it seems nicer having it built in though.
Aug 20 2004
clayasaurus wrote:Hello. I know we already have ~= for character arrays and int arrays. However they don't work for class/struct arrays and character arrays of arrays.Can you post an example? I just tried char[][] items; items ~= "item1"; items ~= "item2"; printf("%d %.*s %.*s\n",items.length,items[0],items[1]); and got the (correct) answer 2 item1 item2Right now, If I want to add something to a class or array of character arrays i have do to this... // this is what I do items.length = items.length+1; // first I add one to the array items[length-1] = "string"; // then I set the last index to what I want What I'd like to see is an operator that works like push_back() in c++ vectors. Where all you'd have to type is items &&= "string"; // i think &= is already used? or items pushback "string"; or or something to that effect, i'm not sure about what operator would fit nicely. What do you think? Or should we just leave this up to templates (DTL?) with a items.pushback("string"); syntax? it seems nicer having it built in though.
Aug 20 2004
Ben Hinkle wrote:clayasaurus wrote:Aha. Well what I'd like is just to do char[] items; items ~= "item1"; items ~= "item2"; printf("%d %.*s %.*s\n",items.length,items[0],items[1]); ang get the correct answer. with this i get seg fault yay. withouth the ~= operator, of course.Hello. I know we already have ~= for character arrays and int arrays. However they don't work for class/struct arrays and character arrays of arrays.Can you post an example? I just tried char[][] items; items ~= "item1"; items ~= "item2"; printf("%d %.*s %.*s\n",items.length,items[0],items[1]); and got the (correct) answer 2 item1 item2
Aug 20 2004
clayasaurus wrote:Ben Hinkle wrote:This can hold only one string.clayasaurus wrote:Aha. Well what I'd like is just to do char[] items;Hello. I know we already have ~= for character arrays and int arrays. However they don't work for class/struct arrays and character arrays of arrays.Can you post an example? I just tried char[][] items; items ~= "item1"; items ~= "item2"; printf("%d %.*s %.*s\n",items.length,items[0],items[1]); and got the (correct) answer 2 item1 item2items ~= "item1"; items ~= "item2"; printf("%d %.*s %.*s\n",items.length,items[0],items[1]);item[0], item[1] refernce the first and second character. printing in string format uses character as adressesang get the correct answer. with this i get seg fault yay.yes, a fault is to be expected.withouth the ~= operator, of course.-- Helmut Leitner leitner hls.via.at Graz, Austria www.hls-software.com
Aug 20 2004
"clayasaurus" <clayasaurus gmail.com> wrote in message news:cg6h7a$15h3$1 digitaldaemon.com...Hello. I know we already have ~= for character arrays and int arrays. However they don't work for class/struct arrays and character arrays of arrays.But ~= works for all arrays. class Foo {} int main() { char[][] strings; strings ~= "mystring"; strings ~= "foo"; strings ~= "etc"; Foo[] fa; fa ~= new Foo; return 0; } Or am I misunderstanding you?
Aug 20 2004
Vathix wrote:"clayasaurus" <clayasaurus gmail.com> wrote in message news:cg6h7a$15h3$1 digitaldaemon.com...Maybe I am just dumb or something. But anyway import std.stdio; struct Bob { char[] bob; } int main(char[][] args) { Bob[] bob; Bob bob1, bob2; bob1.bob = "jim"; bob2.bob = "bob"; bob ~= bob1 ~= bob2; // this does not work //bob.length = 2; // this does //bob[0] = bob1; //bob[1] = bob2; writefln(bob[0].bob); writefln(bob[1].bob); return 0; } I get the error "Can only concatenate arrays," but is bob not an array? *confused*Hello. I know we already have ~= for character arrays and int arrays. However they don't work for class/struct arrays and character arrays of arrays.But ~= works for all arrays. class Foo {} int main() { char[][] strings; strings ~= "mystring"; strings ~= "foo"; strings ~= "etc"; Foo[] fa; fa ~= new Foo; return 0; } Or am I misunderstanding you?
Aug 20 2004
"clayasaurus" <clayasaurus gmail.com> wrote in message news:cg6i74$16oj$1 digitaldaemon.com...Vathix wrote:To do what you want to do write: bob ~= bob1; bob ~= bob2; Although i would think taht this would work too: (bob ~= bob1) ~= bob2; but it doesn't :("clayasaurus" <clayasaurus gmail.com> wrote in message news:cg6h7a$15h3$1 digitaldaemon.com...Maybe I am just dumb or something. But anyway import std.stdio; struct Bob { char[] bob; } int main(char[][] args) { Bob[] bob; Bob bob1, bob2; bob1.bob = "jim"; bob2.bob = "bob"; bob ~= bob1 ~= bob2; // this does not workHello. I know we already have ~= for character arrays and int arrays. However they don't work for class/struct arrays and character arrays of arrays.But ~= works for all arrays. class Foo {} int main() { char[][] strings; strings ~= "mystring"; strings ~= "foo"; strings ~= "etc"; Foo[] fa; fa ~= new Foo; return 0; } Or am I misunderstanding you?//bob.length = 2; // this does //bob[0] = bob1; //bob[1] = bob2; writefln(bob[0].bob); writefln(bob[1].bob); return 0; } I get the error "Can only concatenate arrays," but is bob not an array? *confused*
Aug 21 2004
I guess my confusion arouse out of thinking bob ~= bob1 ~= bob2; would be the same as bob ~= bob1; bob ~= bob2; and I got confused on the difference between char[] and char[][]. Thanks for all the replies! Ivan Senji wrote:"clayasaurus" <clayasaurus gmail.com> wrote in message news:cg6i74$16oj$1 digitaldaemon.com...Vathix wrote:To do what you want to do write: bob ~= bob1; bob ~= bob2; Although i would think taht this would work too: (bob ~= bob1) ~= bob2; but it doesn't :("clayasaurus" <clayasaurus gmail.com> wrote in message news:cg6h7a$15h3$1 digitaldaemon.com...Maybe I am just dumb or something. But anyway import std.stdio; struct Bob { char[] bob; } int main(char[][] args) { Bob[] bob; Bob bob1, bob2; bob1.bob = "jim"; bob2.bob = "bob"; bob ~= bob1 ~= bob2; // this does not workHello. I know we already have ~= for character arrays and int arrays. However they don't work for class/struct arrays and character arrays of arrays.But ~= works for all arrays. class Foo {} int main() { char[][] strings; strings ~= "mystring"; strings ~= "foo"; strings ~= "etc"; Foo[] fa; fa ~= new Foo; return 0; } Or am I misunderstanding you?//bob.length = 2; // this does //bob[0] = bob1; //bob[1] = bob2; writefln(bob[0].bob); writefln(bob[1].bob); return 0; } I get the error "Can only concatenate arrays," but is bob not an array? *confused*
Aug 21 2004
"clayasaurus" <clayasaurus gmail.com> wrote in message news:cg7h9v$1vfu$1 digitaldaemon.com...I guess my confusion arouse out of thinking bob ~= bob1 ~= bob2; would be the same as bob ~= bob1; bob ~= bob2;Quite understandable. I fail to see why it should not. If ~= does not return its lhs as an lvalue, then what on earth _does_ it return? Most confusing.and I got confused on the difference between char[] and char[][]. Thanks for all the replies! Ivan Senji wrote:"clayasaurus" <clayasaurus gmail.com> wrote in message news:cg6i74$16oj$1 digitaldaemon.com...Vathix wrote:To do what you want to do write: bob ~= bob1; bob ~= bob2; Although i would think taht this would work too: (bob ~= bob1) ~= bob2; but it doesn't :("clayasaurus" <clayasaurus gmail.com> wrote in message news:cg6h7a$15h3$1 digitaldaemon.com...Maybe I am just dumb or something. But anyway import std.stdio; struct Bob { char[] bob; } int main(char[][] args) { Bob[] bob; Bob bob1, bob2; bob1.bob = "jim"; bob2.bob = "bob"; bob ~= bob1 ~= bob2; // this does not workHello. I know we already have ~= for character arrays and int arrays. However they don't work for class/struct arrays and character arrays of arrays.But ~= works for all arrays. class Foo {} int main() { char[][] strings; strings ~= "mystring"; strings ~= "foo"; strings ~= "etc"; Foo[] fa; fa ~= new Foo; return 0; } Or am I misunderstanding you?//bob.length = 2; // this does //bob[0] = bob1; //bob[1] = bob2; writefln(bob[0].bob); writefln(bob[1].bob); return 0; } I get the error "Can only concatenate arrays," but is bob not an array? *confused*
Aug 21 2004
Matthew wrote:"clayasaurus" <clayasaurus gmail.com> wrote in message news:cg7h9v$1vfu$1 digitaldaemon.com...... void, I think. (I must admit to not having tried this, but it's one of two standard conventions. The other would have it returning a string, which would allow: if ((bob ~= bob) == "bob) then... But I seem to remember an explicit decision against that. If you want that try: bob ~= bob1 ~ bob2; But this wouldn't change the value of bob1. And bob had better already have a legal value (would null work here? Probably).I guess my confusion arouse out of thinking bob ~= bob1 ~= bob2; would be the same as bob ~= bob1; bob ~= bob2;Quite understandable. I fail to see why it should not. If ~= does not return its lhs as an lvalue, then what on earth _does_ it return? Most confusing.
Aug 23 2004
"clayasaurus" <clayasaurus gmail.com> wrote in message news:cg6h7a$15h3$1 digitaldaemon.com...Hello. I know we already have ~= for character arrays and int arrays. However they don't work for class/struct arrays and character arrays of arrays. Right now, If I want to add something to a class or array of character arrays i have do to this... // this is what I do items.length = items.length+1; // first I add one to the array items[length-1] = "string"; // then I set the last index to what I want What I'd like to see is an operator that works like push_back() in c++ vectors. Where all you'd have to type is items &&= "string"; // i think &= is already used? or items pushback "string"; or or something to that effect, i'm not sure about what operator would fit nicely.I'm more than a little surprised that ~= does not work for arrays of all types. Are you sure that's correct? (Not casting aspersions; just finding hard to believe)What do you think? Or should we just leave this up to templates (DTL?) with a items.pushback("string"); syntax? it seems nicer having it built in though.I think ~= should work for all arrays.
Aug 20 2004
Matthew wrote:I'm more than a little surprised that ~= does not work for arrays of all types. Are you sure that's correct? (Not casting aspersions; just finding hard to believe)What I would like (like push back in c++ vector) is for it to add one to the array length and then fill it with given value. like char[] items; items add "bob"; items add "monkey"; and have items become an array the length of 2 with items[0] = "bob" and items[1] = "monkey"
Aug 20 2004
clayasaurus wrote:Hello. I know we already have ~= for character arrays and int arrays. However they don't work for class/struct arrays and character arrays of arrays. Right now, If I want to add something to a class or array of character arrays i have do to this... // this is what I do items.length = items.length+1; // first I add one to the array items[length-1] = "string"; // then I set the last index to what I want What I'd like to see is an operator that works like push_back() in c++ vectors. Where all you'd have to type is items &&= "string"; // i think &= is already used? or items pushback "string"; or or something to that effect, i'm not sure about what operator would fit nicely. What do you think? Or should we just leave this up to templates (DTL?) with a items.pushback("string"); syntax? it seems nicer having it built in though.The trick is that ~= appends a single element to an array, whereas ~ combines two arrays: Spam[] ni; Spam[] eggs; eggs ~= new Spam(); // ok eggs = ni ~ ni ~ ni; // ok eggs ~= ni; // no eggs = eggs ~ new Spam(); // no It's inconsistent, but it's better than the alternative: Spam temp = new Spam(); eggs ~= &(temp)[0..1] :) -- andy
Aug 20 2004
In article <cg6k22$18s7$1 digitaldaemon.com>, Andy Friesen says...The trick is that ~= appends a single element to an array, whereas ~ combines two arrays: Spam[] ni; Spam[] eggs; eggs ~= new Spam(); // ok eggs = ni ~ ni ~ ni; // ok eggs ~= ni; // no eggs = eggs ~ new Spam(); // noIs that really true? Wow. I had assumed that was equivalent to: Are you saying it isn't?It's inconsistent, but it's better than the alternative: Spam temp = new Spam(); eggs ~= &(temp)[0..1]It's inconsistent, and therefore I would never have guessed in a million years that it might work. Hell, for all I know now, maybe += and + behave differently from each other. Do I have to try them all to find out? I've always done: My preference would be that /both/ ~ /and/ ~= should be overloaded, in the obvious way. For all types T: with ~= behaving identically. I don't think this leads to any ambiguity, does it? Arcane Jill
Aug 20 2004
Arcane Jill wrote:It's inconsistent, and therefore I would never have guessed in a million years that it might work. Hell, for all I know now, maybe += and + behave differently from each other. Do I have to try them all to find out? I've always done: My preference would be that /both/ ~ /and/ ~= should be overloaded, in the obvious way. For all types T: with ~= behaving identically. I don't think this leads to any ambiguity, does it?In any setting where an array can contain an element of its own type, yes. (dynamic types, arrays inheriting Object, et cetera) That is never the case in D now, so it should be okay. It basically boils down to whether or not D wants to keep those sorts of possibilities open. Personally, I think ~ and ~= should concatenate arrays *only*. Array literal syntax covers the rest: Spam[] spam, eggs; spam ~= eggs; // ok now spam ~= [new Spam()]; // concat anon array of 1 spam = [new Spam(), new Spam()]; // concatenate two elements into one array -- andy
Aug 21 2004
Andy Friesen schrieb:Personally, I think ~ and ~= should concatenate arrays *only*. Array literal syntax covers the rest: Spam[] spam, eggs; spam ~= eggs; // ok now spam ~= [new Spam()]; // concat anon array of 1 spam = [new Spam(), new Spam()]; // concatenate two elements into one arrayAgree and vote! -eye
Aug 23 2004