Comparison of programming languages (associative array): Difference between revisions

Content deleted Content added
Perl: Add Perl 6 section
Line 845:
</source>
 
=== [[Perl 5]] ===
Perl 5 has built-in, language-level support for associative arrays. Modern Perl vernacular refers to associative arrays as ''hashes''; the term ''associative array'' is found in older documentation, but is considered somewhat archaic. Perl 5 hashes are flat: keys are strings and values are scalars. However, values may be [[reference (computer science)|references]] to arrays or other hashes, and the standard Perl 5 module Tie::RefHash enables hashes to be used with reference keys.
 
A hash variable is marked by a <code>%</code> [[sigil (computer programming)|sigil]], to distinguish it from scalar, array and other data types. A hash literal is a key-value list, with the preferred form using Perl's <code>=&gt;</code> token, which is mostly semantically identical to the comma and makes the key-value association clearer:
Line 898:
print 'Number for ', $name, ': ', $phone_book->{$name}, "\n";
}
</source>
 
=== [[Perl 6]] ===
Perl 6 also has built-in, language-level support for associative arrays, which are referred to as ''hashes'' or as objects performing the ''Associative'' role. By default, Perl 6 hashes are flat: keys are strings and values are scalars. One can define a ''hash'' to be not automatically coerce all keys to strings: these are referred to as ''object hashes'', because the keys of such hashes remain the original object rather than a stringification thereof.
 
A hash variable is typically marked by a <code>%</code> [[sigil (computer programming)|sigil]], to visually distinguish it from scalar, array and other data types, and to define its behaviour towards iteration. A hash literal is a key-value list, with the preferred form using Perl's <code>=&gt;</code> token, which makes the key-value association clearer:
 
<source lang=Perl6>
%phone-book =
'Sally Smart' => '555-9999',
'John Doe' => '555-1212',
'J. Random Hacker' => '553-1337',
;
</source>
 
Accessing a hash element uses the syntax <code>%hash_name{$key}</code> – the key is surrounded by ''curly braces'' and the hash name (note that the sigil does '''not''' change, contrary to [[Perl 5]]). The value of <code>%phone-book{'John Doe'}</code> is <code>'555-1212'</code>.
 
The list of keys and values can be extracted using the built-in functions <code>keys</code> and <code>values</code>, respectively. So, for example, to print all the keys of a hash:
 
<source lang=Perl6>
for %phone-book.keys -> $name {
say $name;
}
</source>
 
By default, when iterating over a ''hash'', one gets key/value ''Pair''s.
 
<source lang=Perl6>
for %phone-book -> $entry {
say "Number for $entry.key(): $entry.value()"; # using extended interpolation features
}
</source>
 
It's also possible to get alternating key and value values by using the ''kv'' method:
 
<source lang=Perl6>
for %phone-book.kv -> $name, $number {
say "Number for $name: $number";
}
</source>
 
[[Perl 6]] doesn't have any references. Hashes can be passed around as single parameters that do not get flattened. If you want to make sure that a subroutine only accepts hashes, use the ''%'' sigil in the ''Signature''.
 
<source lang=Perl6>
sub list-phone-book(%pb) {
for %pb.kv -> $name, $number {
say "Number for $name: $number";
}
}
list-phone-book(%phone-book);
</source>