Your skill will accomplishwhat the force of many cannot PerlMonks

### Arrays: A Tutorial/Reference

by jdporter (Canon)
 on Jan 27, 2009 at 19:08 UTC Need Help??

```@array = ( 1, 2, 3 );
@array = function_generating_a_list();
@array = @another_array;
```
```@array = ( 'a', 'b', 'c' );
```
```@array = ();
```
```@array = 0;
# and
@array = ( 0 );
```
```\$count = @array;
```
```print "# Elements: " . @array . "\n";
```
```print "# Elements: ", scalar(@array), "\n";
```
```\$highest_index = \$#array;
```
```foreach ( 0 .. \$#array ) {
# \$_ is set to each index number, in turn, from first (0) to last (\$
+#array)
}
```
```print "Here are your things: ", @array, "\n";
```
```foreach ( @array ) { ...
```
```\$x = @array;
@x = @array;
```
```@array = ( 'a', 'b', 'c' );
\$x = pop @array;
```
```push @array, 8, 10 .. 15;
```
```@array = ( 'a', 'b', 'c' );
\$x = shift @array;
```
```@array = ( 1, 2 );
unshift @array, 'y', 'z';
```
```\$first_elem = \$array[0];
```
```\$array[ \$#array ] += 5;
```
```( \$first, \$third, \$fifth ) = @array[0,2,4];
```
```\$n = @array[0..\$#array];
```
```@array[1..3] = ( 'x', 'y', 'z' );
```
```\$array[ -1 ]
\$array[ \$#array ]
```
```@array[ 0 .. \$#array ]
```
```@array[ 0 .. -1 ]
```
```unshift @a, @b;
# could be written as
splice  @a, 0, 0, @b;
```
```push @a, @b;
# could be written as
splice @a, \$#a+1, 0, @b; # we have to index to a position PAST the end
+ of array!
```
```\$b = shift @a;
# could be written as
\$b = splice  @a, 0, 1;
```
```\$b = pop @a;
# could be written as
\$b = splice  @a, -1, 1;
```
```@b = splice @a, 2, 3;
```
```splice @a, 2, 0, @b;
```
```splice @a,         # array to modify
3,             # starting with 4th item
2,             # remove (replace) two items
'x', 'y', 'z'; # arbitrary list of new values to insert
```
```@a = ();
# could be written as
splice @a, 0;
```

Create A New User
Node Status?
node history
Node Type: perltutorial [id://739305]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2021-04-13 04:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?

No recent polls found

Notices?