Line 158: |
Line 158: |
| ==== Hashes ==== | | ==== Hashes ==== |
| | | |
− | A hash represents a set of key/value pairs:
| + | A hash represents a set of key/value pairs: |
| | | |
| my %fruit_color = ("apple", "red", "banana", "yellow"); | | my %fruit_color = ("apple", "red", "banana", "yellow"); |
| | | |
− | You can use whitespace and the => operator to lay them out more nicely:
| + | You can use whitespace and the '''=>''' operator to lay them out more nicely: |
| | | |
| my %fruit_color = ( | | my %fruit_color = ( |
Line 169: |
Line 169: |
| ); | | ); |
| | | |
− | To get at hash elements:
| + | To get at hash elements: |
| | | |
| $fruit_color{"apple"}; # gives "red" | | $fruit_color{"apple"}; # gives "red" |
− | | + | You can get at lists of keys and values with '''keys()''' and '''values()'''. |
− | You can get at lists of keys and values with keys() and values().
| |
| | | |
| my @fruits = keys %fruit_colors; | | my @fruits = keys %fruit_colors; |
| my @colors = values %fruit_colors; | | my @colors = values %fruit_colors; |
| | | |
− | Hashes have no particular internal order, though you can sort the keys and loop through them.
| + | Hashes have no particular internal order, though you can sort the keys and loop through them. |
| | | |
− | Just like special scalars and arrays, there are also special hashes. The most well known of these is %ENV which contains environment variables. Read all about it (and other special variables) in perlvar.
| + | Just like special scalars and arrays, there are also special hashes. The most well known of these is '''%ENV''' which contains environment variables. Read all about it (and other special variables) in [http://perldoc.perl.org/perlvar.html perlvar]. |
| | | |
− | Scalars, arrays and hashes are documented more fully in perldata. | + | Scalars, arrays and hashes are documented more fully in [http://perldoc.perl.org/perldata.html perldata]. |
| | | |
| More complex data types can be constructed using references, which allow you to build lists and hashes within lists and hashes. | | More complex data types can be constructed using references, which allow you to build lists and hashes within lists and hashes. |
Line 204: |
Line 203: |
| print "Scalars begin with a $variables->{'scalar'}->{'sigil'}\n"; | | print "Scalars begin with a $variables->{'scalar'}->{'sigil'}\n"; |
| | | |
− | Exhaustive information on the topic of references can be found in perlreftut, perllol, perlref and perldsc. | + | Exhaustive information on the topic of references can be found in [http://perldoc.perl.org/perlreftut.html perlreftut], [http://perldoc.perl.org/perllol.html perllol], [http://perldoc.perl.org/perlref.html perlref] and [http://perldoc.perl.org/perldsc.html perldsc]. |
| + | |
| === Variable scoping === | | === Variable scoping === |
| | | |