Quick Navigation Bar input/output :: records and references :: regular expressions [ toc | forums ] |
Note: If the document URL does not begin with https://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!
Notice: Please do not replicate or copy these pages and
host them elsewhere. This is to ensure that the latest version can always
be found here.
Disclaimer: The document author has published these pages
with the hope that it may be useful to others. However, the document
author does not guarantee that all information contained on these
webpages are correct or accurate. There is no warranty, expressed or
implied, of merchantability or fitness for any purpose. The author does
not assume any liability or responsibility for the use of the information
contained on these webpages.
If you see an error, please send an email to the address below indicating
the error. Your feedback is greatly appreciated and will help to
continually improve these pages.