В Perl нет многомерных массивов.Вы можете эмулировать такое поведение с массивом ссылок на массивы.
@foo = ('one','two');
@bar = ('three', 'four');
# equivalent of @baz = ('one','two','three', 'four');
@baz = (@foo, @bar);
# you need to store array references in @baz:
@baz = (\@foo, \@bar);
# Perl have a shortcut for such situations (take reference of all list elements):
# @baz = \(@foo, @bar);
# so, we have array ref as elements of @baz;
print "First element: $baz[0]\n";
print "Second element: $baz[1]\n";
# references must be dereferenced with dereferencing arrow
print "$baz[0]->[0]\n";
# -1 is a shortcut for last array element
print "$baz[1]->[-1]\n";
# but Perl knows that we can nest arrays ONLY as reference,
# so dereferencing arrow can be omitted
print "$baz[1][0]\n";
Списки эфемерны и существуют только там, где они определены.Вы не можете хранить сам список, но значения списка могут быть сохранены, поэтому списки не могут быть вложенными.(1,2, (3,4)) просто безобразно эквивалентен (1,2,3,4)
Но вы можете взять часть списка следующим образом:
print(
join( " ", ('garbage', 'apple', 'pear', 'garbage' )[1..2] ), "\n"
);
Этот синтаксис не имеет смысла, если @structure определен как массив скалярных значений:
my @structure = (@ana,@dana,@mihai,@daniel);
@{$structure[3]}[2];
Вы пытаетесь разыменовать строку.Всегда используйте прагмы stict и warnings в вашем коде, и вы будете свободны от таких ошибок:
# just try to execute this code
use strict;
use warnings;
my @ana = ("Godfather", "Dirty Dancing", "Lord of the Rings", "Seven", "Titanic");
my @structure = (@ana);
print @{$structure[0]}, "\n";
Правильное использование:
use strict;
use warnings;
my @ana = ("Godfather\n", "Dirty Dancing\n", "Lord of the Rings\n", "Seven\n", "Titanic\n");
my @structure = (\@ana);
# dereference array at index 0, get all it's elements
print @{$structure[0]};
print "\n";
# silly, one element slice, better use $structure[0][1];
print @{$structure[0]}[1];
print "\n";
# more sense
print @{$structure[0]}[2..3];
Вы можете прочитать больше здесь:
perldoc perlref
perldoc perllol
Документация для Perl - лучшая документация, которую я когда-либо видел, посмотрите и повеселитесь!