Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename arrays concept to arrays-and-lists #596

Merged
merged 6 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
65 changes: 65 additions & 0 deletions concepts/arrays-and-lists/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Introduction

Arrays and lists contain an ordered collection of scalars.
Outside of using references (which will be covered in a separate concept), this means that arrays and lists are always flat, i.e., they cannot contain other arrays or lists.

In most cases, lists are surrounded by parentheses `()` with their elements separated by commas `,`.

An array variable is usually populated by assigning a list to it, e.g., `my @names = ('Alice', 'Bob', 'Charlie');`.

Elements of an array or list can be accessed by specifying indexes via a subscript, e.g., `('Alice', 'Bob', 'Charlie')[1] # 'Bob'`.

Indexes in Perl start at `0`.
Negative indexes will count from the end of the array or list, e.g., `('Alice', 'Bob', 'Charlie')[-1] # 'Charlie'`.

## Functions

There are many functions implemented for operating on arrays and lists, some of the most useful being [grep][grep] for a new list of filtered elements which match a condition, [map][map] for translating elements to a new list of values, and [sort][sort] for a new list of reordered elements.

Functions for manipulating arrays include [push][push] to add elements to the end, [pop][pop] to remove the last element, [shift][shift] to remove the first element, [unshift][unshift] to add elements to the start, and [splice][splice] for more complex modifications.

## Sigil Variance

When accessing the elements of an array, the given sigil determines the result.
The subscript `[]` at the end of the variable name is what tells Perl that you are accessing an array with that name.

```perl
my @names = ('Alice', 'Bob', 'Charlie');

$names[0]; # 'Alice' <- A scalar
@names[0]; # ('Alice') <- A slice (with only one index, this probably isn't what you want. There will be a warning.)
%names[0]; # (0, 'Alice') <- A key/index + value slice

$names[1, 2]; # 'Charlie' <- A scalar (via unsupported syntax for an ancient feature. Probably not what you want. There will be a warning.)
@names[1, 2]; # ('Bob', 'Charlie') <- A slice
%names[1, 2]; # (1, 'Bob', 2, 'Charlie') <- A key/index + value slice
```

While it is allowed, it is recommended to not have different variable types share the same name for the sake of readability.

## Arrays vs Lists

Arrays and lists share many similarities, and in a lot of places can be used interchangably, but they are not the same thing.
A list is a fixed and immutable collection of scalars.
An array is a variable collection of scalars where the contents and the size of the array itself can be changed.

They have different behaviors in some contexts, for example, an array used in scalar context will give you the size of the array, while a list used in scalar context will give you the last element in the list, and potentially some warnings about the rest of its contents being used in void context.

```perl
my @names = ('Alice', 'Bob', 'Charlie');

scalar @names; # 3
scalar ('Alice', 'Bob', 'Charlie'); # 'Charlie' (and some warnings).
```

Read more in the folowing FAQ: [What is the difference between a list and an array?](perlfaq4listarray)

[perlfaq4listarray]: https://perldoc.pl/perlfaq4#What-is-the-difference-between-a-list-and-an-array?
[grep]: https://perldoc.pl/functions/grep
[map]: https://perldoc.pl/functions/map
[sort]: https://perldoc.pl/functions/sort
[push]: https://perldoc.pl/functions/push
[pop]: https://perldoc.pl/functions/pop
[shift]: https://perldoc.pl/functions/shift
[unshift]: https://perldoc.pl/functions/unshift
[splice]: https://perldoc.pl/functions/splice
26 changes: 26 additions & 0 deletions concepts/arrays-and-lists/links.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[
{
"description": "What is the difference between a list and an array?",
"url": "https://perldoc.pl/perlfaq4#What-is-the-difference-between-a-list-and-an-array?"
},
{
"description": "What is the difference between $array[1] and @array[1]?",
"url": "https://perldoc.pl/perlfaq4#What-is-the-difference-between-$array%5B1%5D-and-@array%5B1%5D?"
},
{
"description": "List Value Constructors",
"url": "https://perldoc.pl/perldata#List-value-constructors"
},
{
"description": "Subscripts",
"url": "https://perldoc.pl/perldata#Subscripts"
},
{
"description": "Slices",
"url": "https://perldoc.pl/perldata#Slices"
},
{
"description": "Index/Value Array Slices",
"url": "https://perldoc.perl.org/perldata#Index/Value-Array-Slices"
}
]
Empty file removed concepts/arrays/introduction.md
Empty file.
1 change: 0 additions & 1 deletion concepts/arrays/links.json

This file was deleted.

6 changes: 3 additions & 3 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"name": "Language List",
"uuid": "073a255f-011e-4264-89ea-69748d5fe71e",
"concepts": [
"arrays"
"arrays-and-lists"
],
"prerequisites": [
"basics"
Expand Down Expand Up @@ -704,8 +704,8 @@
},
{
"uuid": "9bf709d9-0667-4b1c-bd02-9b1c5ee292f9",
"slug": "arrays",
"name": "Arrays"
"slug": "arrays-and-lists",
"name": "Arrays and Lists"
},
{
"uuid": "2d3c030e-0300-4b3e-8e1b-d115b86cdfa1",
Expand Down