|
Quick Navigation Bar input/output :: records and references :: regular expressions [ toc | forums ] |
Note: If the document URL does not begin with http://randu.org/tutorials/perl/ then you are viewing a copy. Please direct your browser to the correct location for the most recent version. |
@arr = (
[ "perl", "rocks" ],
[ "oh", "yeah" ],
[ "hooray", "whoop!"],
);
print "$arr[1][1]\n";
You can also have arrays of hashes, hashes of arrays, and hashes of
hashes. See Wall, Chapter 9: Data Structures for more information.
$foo = "bot" <---+
|
$bar = \$foo ----+
& (address-of) operator in C:
$scalarref = \$somevar; $constref = \3.14; $arrayref = \@ARGV; $hashref = \%somehash;You can even create anonymous data inside of references. These data have no variable reference to them, they are just data sitting inside of a reference:
$arrayref = [ 1, 2, ['a', 'b', 'c', 'd'] ];The internal anonymous array 'a','b','c','d' has no variable associated with it, but can be accessed.
$arrayref->[2][2] would
result in 'c'. The arrow operator here works much like the arrow operator
does in C to dereference.$ operator,
which works much like the * operator does in C:
$foo = "whoop"; $fooref = \$foo; $outcome = $$fooref; # $outcome holds "whoop"You can have references to references (e.g. a pointer to a pointer) in Perl, for each reference, you dereference with an additonal
$.
push(@$arrayref, $filename);
$$arrayref[0] = "January";
@$arrayref[4 .. 6] = qw/May June July/;
%$hashref = (KEY => "RING", BIRD => "SING");
$$hashref{KEY} = "VALUE";
@$hashref{"KEY1", "KEY2"} = ("VAL1", "VAL2");
$rec = {
TEXT => $string,
LISTREF => [ @some_list ],
HASHREF => { %some_hash ],
};
Simple enough, how about accessing fields?
print $rec->{TEXT};
print $rec->{LISTREF}[0];
print $rec->{HASHREF}{"key"};
$rec = {};
while (<>) {
push @readlist, $_;
}
$rec->{LIST} = [@readlist];
Very powerful!
