Mailinglist Archive: yast-devel (77 mails)

< Previous Next >
Re: [yast-devel] Is it allowed to modify one same list via foreach?
  • From: Klaus Kaempf <kkaempf@xxxxxxx>
  • Date: Wed, 2 Dec 2009 15:25:14 +0100
  • Message-id: <20091202142513.GE6688@xxxxxxxxxxxxx>
* Johannes Meixner <jsmeix@xxxxxxx> [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@xxxxxxxxxxxx
For additional commands, e-mail: yast-devel+help@xxxxxxxxxxxx

< Previous Next >