HOME - Recent Changes - Search:

Academic Work


Personal

* pot de départ


dblp


(:twitter:)

-----

[ edit | logout ]
[ help | sandbox | passwd ]

Arrays with References, Dereferences

#############################

Question

How to create an array of arrays? How to process such a data structure? Can you give me some examples?

Answer

Note: 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 @school.

Example 2

How to traverse @school?

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 3

How to access the first class in the school?

print "@{$school[0]}\n";
A1 A2

That is, $school[0] is an array reference that we need to dereference.


More info

Cloud City


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
* Python Adventures
* me @ GitHub


Places

Debrecen | France | Hungary | Montreal | Nancy


Notes

full circle | km


Hobby Projects

* Jabba's Codes
* PmWiki
* Firefox
* PHP
* JavaScript
* Scriptorium
* Tutorials
* me @ GitHub


Quick Links


[ edit ]

View - Edit - History - Attach - Print *** Report - Recent Changes - Search
Page last modified on 2008 June 01, 21:36