[yast-devel] Is it allowed to modify one same list via foreach?
Hello, is such code allowed: -------------------------------------------------------------------- list < string > words = [ "Jane", "John" ]; integer index = -1; foreach( string word, words, { index = index + 1; words[index] = "Hello " + word; } ); -------------------------------------------------------------------- Is in any case the result words == [ "Hello Jane", "Hello John" ] ? If the above is allowed, what about even more evil looking code: -------------------------------------------------------------------- list < string > words = [ "Jane", "John", "Joe" ]; integer index = -1; foreach( string word, words, { index = index + 1; if( "John" == word ) { remove( words, index ); } } ); -------------------------------------------------------------------- Is in any case the result words == [ "Jane", "Joe" ] ? If it is not allowed, how to copy a list? Does -------------------------------------------------------------------- list < string > words = [ "Jane", "John", "Joe" ]; list < string > copy = words; -------------------------------------------------------------------- result two totally separated lists words and copy or does copy contain only a reference which points to words so that -------------------------------------------------------------------- remove( copy, 1 ); -------------------------------------------------------------------- would actually result copy == words == [ "Jane", "Joe" ] ? Kind Regards Johannes Meixner -- SUSE LINUX Products GmbH, Maxfeldstrasse 5, 90409 Nuernberg, Germany AG Nuernberg, HRB 16746, GF: Markus Rex -- To unsubscribe, e-mail: yast-devel+unsubscribe@opensuse.org For additional commands, e-mail: yast-devel+help@opensuse.org
On Wed, Dec 02, 2009 at 02:22:06PM +0100, Johannes Meixner wrote:
Hello,
is such code allowed: -------------------------------------------------------------------- list < string > words = [ "Jane", "John" ]; integer index = -1; foreach( string word, words, { index = index + 1; words[index] = "Hello " + word; } ); -------------------------------------------------------------------- Is in any case the result words == [ "Hello Jane", "Hello John" ] ?
Use maplist: words = maplist(string word, words, { return "Hello " + word; });
If it is not allowed, how to copy a list? Does -------------------------------------------------------------------- list < string > words = [ "Jane", "John", "Joe" ]; list < string > copy = words; -------------------------------------------------------------------- result two totally separated lists words and copy or does copy contain only a reference which points to words so that -------------------------------------------------------------------- remove( copy, 1 ); -------------------------------------------------------------------- would actually result copy == words == [ "Jane", "Joe" ] ?
No, a copy is made. AFAIR even in an efficient way using reference counting. ciao Arvin -- Arvin Schnell, <aschnell@suse.de> Senior Software Engineer, Research & Development SUSE LINUX Products GmbH, GF: Markus Rex, HRB 16746 (AG Nürnberg) -- To unsubscribe, e-mail: yast-devel+unsubscribe@opensuse.org For additional commands, e-mail: yast-devel+help@opensuse.org
Hello, On Dec 2 14:36 Arvin Schnell wrote (shortened):
On Wed, Dec 02, 2009 at 02:22:06PM +0100, Johannes Meixner wrote:
Hello,
is such code allowed: -------------------------------------------------------------------- list < string > words = [ "Jane", "John" ]; integer index = -1; foreach( string word, words, { index = index + 1; words[index] = "Hello " + word; } ); -------------------------------------------------------------------- Is in any case the result words == [ "Hello Jane", "Hello John" ] ?
Use maplist:
words = maplist(string word, words, { return "Hello " + word; });
Regardless of maplist/mapmap: Is my above code using foreach allowed? My actual case is a bit more complicated like: ----------------------------------------------------------------------- list < string > words = [ "Jane", "World", "John" ]; integer index = -1; foreach( string word, words, { index = index + 1; if( "World" == word ) { words[index] = "Hello " + word; } } ); ----------------------------------------------------------------------- Is in any case the result words == [ "Jane", "Hello World", "John" ] ? Assume the list is huge. Wouldn't then ----------------------------------------------------------------------- words = maplist( string word, words, { if( "World" == word ) { return "Hello " + word; } else { return word; } } ); ----------------------------------------------------------------------- consume unnecessarily much memory and CPU (two lists and all elements are copied instead of "in place" modification only where needed via foreach)? Kind Regards Johannes Meixner -- SUSE LINUX Products GmbH, Maxfeldstrasse 5, 90409 Nuernberg, Germany AG Nuernberg, HRB 16746, GF: Markus Rex -- To unsubscribe, e-mail: yast-devel+unsubscribe@opensuse.org For additional commands, e-mail: yast-devel+help@opensuse.org
* Johannes Meixner <jsmeix@suse.de> [Dec 02. 2009 15:10]:
Hello,
On Dec 2 14:36 Arvin Schnell wrote (shortened):
On Wed, Dec 02, 2009 at 02:22:06PM +0100, Johannes Meixner wrote:
Hello,
is such code allowed: -------------------------------------------------------------------- list < string > words = [ "Jane", "John" ]; integer index = -1; foreach( string word, words, { index = index + 1; words[index] = "Hello " + word; } ); -------------------------------------------------------------------- Is in any case the result words == [ "Hello Jane", "Hello John" ] ?
Use maplist:
words = maplist(string word, words, { return "Hello " + word; });
Regardless of maplist/mapmap: Is my above code using foreach allowed?
Yes, iterating over list and changing elements is fine. Changing the list size (adding, removing elements) should also be fine since the index limit is checked against the actual size on every iteration. (Information from top of my head, YMMV !) Klaus --- SUSE LINUX Products GmbH, GF: Markus Rex, HRB 16746 (AG Nürnberg) -- To unsubscribe, e-mail: yast-devel+unsubscribe@opensuse.org For additional commands, e-mail: yast-devel+help@opensuse.org
participants (3)
-
Arvin Schnell
-
Johannes Meixner
-
Klaus Kaempf