|
Academic Work Personal
|
Perl /
Arrays with References, Dereferences
QuestionHow to create an array of arrays? How to process such a data structure? Can you give me some examples? AnswerNote: in this example, under class I mean a normal school class. Example 1
my @school; # a school is a list of classes
# Say we have two classes. Class 1 with 2 students:
my @class1 = ('A1', 'A2');
push(@school, \@class1);
# Class 2 with 3 students:
my @class2 = ('B1', 'B2', 'B3');
push(@school, \@class2);
print "@school\n";
ARRAY(0x81546d8) ARRAY(0x817c06c) Now we have two array references in Example 2How to traverse
my $array_ref; # an array reference
my @class;
foreach $array_ref (@school)
{
@class = @$array_ref; # check out the '@' in front of @$array_ref (dereference)
print "@class\n";
}
A1 A2 B1 B2 B3 Example 3How to access the first class in the school?
print "@{$school[0]}\n";
A1 A2 That is, More info |
![]() anime | bash | blogs | bsd | c/c++ | c64 | calc | comics | convert | cube | del.icio.us | digg | east | eBooks | egeszseg | elite | firefox | flash | fun | games | gimp | google | groovy | hardware | hit&run | howto | java | javascript | knife | lang | latex | liferay | linux | lovecraft | magyar | maths | movies | music | p2p | perl | pdf | photoshop | php | pmwiki | prog | python | radio | recept | rts | scala | scene | sci-fi | scripting | security | shell | space | súlyos | telephone | torrente | translate | ubuntu | vim | wallpapers | webutils | wikis | windows Blogs and Dev. * Ubuntu Incident Places Debrecen | France | Hungary | Montreal | Nancy Notes Hobby Projects * Jabba's Codes Quick Links [ edit ] |