> For the complete documentation index, see [llms.txt](https://trizen.gitbook.io/sidef-lang/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://trizen.gitbook.io/sidef-lang/programming_tasks/c/collections.md).

# Collections

### Array

Arrays are ordered, integer-indexed collections of any object.

```ruby
# creating an empty array and adding values
var a = []          #=> []
a[0] = 1            #=> [1]
a[3] = "abc"        #=> [1, nil, nil, "abc"]
a << 3.14           #=> [1, nil, nil, "abc", 3.14]
```

### Hash

A Hash is a dictionary-like collection of unique keys and their values. Also called associative arrays, they are similar to Arrays, but where an Array uses integers as its index, a Hash allows you to use any object type, which is automatically converted into a String.

```ruby
# creating an empty hash
var h = Hash()    #=> Hash()
h{:foo} = 1       #=> Hash("foo"=>1)
h{:bar} = 2.4     #=> Hash("foo"=>1, "bar"=>2.4)
h{:bar} += 3      #=> Hash("foo"=>1, "bar"=>5.4)
```

### Pair

A Pair is an array-like collection, but restricted only to two elements.

```ruby
# create a simple pair
var p = Pair('a', 'b')
say p.first;            #=> 'a'
say p.second;           #=> 'b'
 
# create a pair of pairs
var pair = 'foo':'bar':'baz':();   # => Pair('foo', Pair('bar', Pair('baz', nil)))
 
# iterate over the values of a pair of pairs
loop {
    say pair.first;                #=> 'foo', 'bar', 'baz'
    pair = pair.second;
    pair == nil && break;
}
```

### Struct

A Struct is a convenient way to bundle a number of attributes together.

```ruby
# creating a struct
struct Person {
    String name,
    Number age,
    String sex
}
 
var a = Person("John Smith", 41, :man)
 
a.age += 1                  # increment age
a.name = "Dr. #{a.name}"    # update name
 
say a.name          #=> "Dr. John Smith"
say a.age           #=> 42
say a.sex           #=> "man"
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://trizen.gitbook.io/sidef-lang/programming_tasks/c/collections.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
