म
MarathiCodeby Sprout Tech
Documentation topicsList Indexing & Mutation
MarathiCode v0.2.0

List Indexing & Mutation

यादी[निर्देशांक]

Reading items

Items are numbered from 0. x[i] reads item i, and indexes can be chained for nested lists. Any expression that gives a whole number can be used as an index.

indexing.mr
चल संख्या = [10, 20, 30]
छापा(संख्या[0])
छापा(संख्या[लांबी(संख्या) - 1])

चल matrix = [[1, 2], [3, 4]]
छापा(matrix[1][0])
Output (आउटपुट)
10
30
3

Changing items

Assign to an index to replace an item. This also works on nested lists:

mutation.mr
चल संख्या = [10, 20, 30]
संख्या[1] = 99
छापा(संख्या)

चल matrix = [[1, 2], [3, 4]]
matrix[0][1] = 50
छापा(matrix)
Output (आउटपुट)
[10, 99, 30]
[[1, 50], [3, 4]]

Changing every item in a loop

double_all.mr
चल गुण = [5, 8, 3]
साठी (i = 0; i < लांबी(गुण); i = i + 1) {
    गुण[i] = गुण[i] * 2
}
छापा(गुण)
Output (आउटपुट)
[10, 16, 6]

Rules

  • An index must be a whole number from 0 to लांबी(x) - 1. Negative indexes are not allowed.
  • Indexing past the end, reading or writing, is a runtime error.
  • Strings can be indexed for reading but not changed.
  • To add an item, use जोडा. There is no slicing or removal of items.
out_of_range.mr
चल यादी = [1, 2, 3]
छापा(यादी[3])
Output / Error (आउटपुट / त्रुटी)
धावपळ त्रुटी (Runtime Error)
ओळ 2:
    छापा(यादी[3])
यादीची निर्देशांक मर्यादा ओलांडली (Index 3, length 3).