Difference between revisions of "Array Reference"

Jump to navigation Jump to search
464 bytes added ,  19:27, 1 September 2013
added new warning about problem with array.find()
imported>Egocarib
imported>Egocarib
(added new warning about problem with array.find())
Line 16: Line 16:
MyObject[]
MyObject[]
</source>
</source>
 
<br>
== Array Creation ==
== Array Creation ==
  <array creation> ::= 'new' <element type> '[' <int> ']'
  <array creation> ::= 'new' <element type> '[' <int> ']'
Line 38: Line 38:
MyScript[] x = new MyScript[5]
MyScript[] x = new MyScript[5]
</source>
</source>
 
<br>
== Array Length ==
== Array Length ==
  <array length> ::= <expression> '.' 'Length'
  <array length> ::= <expression> '.' 'Length'
Line 56: Line 56:
x = myArray.Length ; x will get 0
x = myArray.Length ; x will get 0
</source>
</source>
 
<br>
== Array Elements ==
== Array Elements ==
  <array access> ::= <expression> '[' <expression> ']'
  <array access> ::= <expression> '[' <expression> ']'


To get a specific array element, just put the index of the element you want between two square brackets. This value can also come from an [[Expression Reference|expression]], or a return value of a [[Function Reference|function]], or something else. The valid elements in an array are from 0 to the length minus 1.
To get a specific array element, just put the index of the element you want between two square brackets. This value can also come from an [[Expression Reference|expression]], or a return value of a [[Function Reference|function]], or something else (see Warnings below for an exception). The valid elements in an array are from 0 to the length minus 1.


=== Examples ===
=== Examples ===
Line 72: Line 72:
x = myArray[myArray.Length - 1]
x = myArray[myArray.Length - 1]
</source>
</source>
 
<br>
== Warnings ==
== Warnings ==
Only the = operator can be used with an [[Arrays_(Papyrus)|array]]:
*Only the = operator can be used with an [[Arrays_(Papyrus)|array]] (see [[Operator_Reference#Assignment_Operators|Assignment Operators]]):
<source lang="papyrus">
<source lang="papyrus">
  ; Will not compile, as the compiler doesn't know how to handle it.
  ; Will not compile, as the compiler doesn't know how to handle it.
  myArray[3] += 5
  myArray[3] += 5
</source>
</source>
(see [[Operator_Reference#Assignment_Operators|Assignment Operators]])
<br>
*The array Find() function cannot be used within an array's element index brackets
<source lang="papyrus">
;this works fine:
i = myArray.Find(none)  ;(find the first blank element)
myArray[i] = newValue  ;(and fill it with our newValue)


;this will compile, but it will not work correctly in the script, and the value will not be filled as expected:
myArray[myArray.Find(none)] = newValue
</source>
<br>
[[Category:Scripting]]
[[Category:Scripting]]
[[Category:Papyrus]]
[[Category:Papyrus]]
[[Category:Papyrus Language Reference]]
[[Category:Papyrus Language Reference]]
Anonymous user

Navigation menu