首页 > 解决方案 > 另一个数组中的数组perl

问题描述

my @ana = ("Godfather", "Dirty Dancing", "Lord of the Rings", "Seven", "Titanic");
my @dana = ("American Pie", "Harry Potter", "Bruce Almighty", "Jaws 1", "Solaris");
my @mihai = ("Fight Club", "Gladiator", "Troy", "Eternal Sunshine of the Spotless Mind", "Lord of the Rings");
my @daniel = ("Independence Day", "Finding Nemo", "Gladiator", "Godfather", "Schindler’s List");

my @structure = (@ana,@dana,@mihai,@daniel);

如何从@structure 获取一部电影?

my $subarray = @{$structure[3]}[3];

此行不起作用,我需要有关此语法的更多信息

标签: perl

解决方案


数组在列表上下文中被展平,因此您的@structure 包含@ana 的元素,后跟@dana 的元素等。使用数组引用嵌套数组:

my @structure = (\@ana, \@dana, \@mihai, \@daniel);
my $movie = $structure[3][3];

推荐阅读