Reputation: 126
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);
how to get a single movie from @structure?
my $subarray = @{$structure[3]}[3];
this line dont work, and i need more information about this syntax
Upvotes: 0
Views: 109
Reputation: 687
There are no multidimensional arrays in Perl. You can emulate such behavior with array of array references.
@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";
Lists are ephemeral, and exists only where they defined. You can't store list itself, but values of list can be stored, that's why lists can't be nested. (1,2,(3,4)) is just ugly equivalent of (1,2,3,4)
But you can take a slice of a list this way:
print(
join( " ", ('garbage', 'apple', 'pear', 'garbage' )[1..2] ), "\n"
);
This syntax have no sense if @structure defined as array of scalar values:
my @structure = (@ana,@dana,@mihai,@daniel);
@{$structure[3]}[2];
You are trying to dereference string. Always use stict and warnings pragmas in your code and you will be free of such errors:
# 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";
Correct usage:
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];
You can read more here:
perldoc perlref
perldoc perllol
Documentation for Perl is the best documentation I ever see, take a look and have fun!
Upvotes: 1
Reputation: 69314
To back up choroba's answer, you can get more information about about building complex data structures in Perl from perldoc perllol and perldoc perldsc.
Upvotes: 1
Reputation: 242343
Arrays are flattened in list context, so your @structure contains the elements of @ana followed by the elements of @dana, etc. Use array references to nest arrays:
my @structure = (\@ana, \@dana, \@mihai, \@daniel);
my $movie = $structure[3][3];
Upvotes: 5