Prev | Current Page 186 | Next

L. McColl-Sylvester and F. Ponticelli

"Professional haXe and Neko"

This can be a little problematic if
you want to modify the values stored in the Array of, say, strings but want those modifications to
perpetuate in the Array once the loop has ended. Take a look at the next example:
var myArray = [???item1???, ???item2???, ???item3???];
for ( i in myArray )
{
i += ???0???;
trace( i );
}
trace( myArray[0] );
// Outputs: item1
When the values are written to screen, you can see that each of the items in the Array has been appended
with the character 0, yet when the first item of the Array is queried outside of the for loop, it appears
without the appended character. Sometimes, this may very well be the effect you wanted, but what if
it ??™ s not?
Under such circumstances, you can abstract the use of an iterator by using the IntIter object and
setting its maximum value to the length of the Array. This way, you can directly access the Array items
and guarantee that the items are modified. Using this method, your previous example will look like this:
var myArray = [???item1???, ???item2???, ???item3???];
for ( i in 0.


Pages:
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198