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

Content deleted Content added
Perl 5: copy editing
copy editing "Raku" subsection (renamed "Perl 6 (Raku)")
Line 1,269:
</syntaxhighlight>
 
===Perl [[Raku6 (programming languageRaku)|Raku]] ===
[[Raku (programming language)|Perl 6]], renamed as "Raku", also has built-in, language-level support for associative arrays, which are referred to as ''hashes'' or as objects performing the ''Associative''"associative" role. ByAs defaultin Perl 5, Perl 6 default hashes are flat: keys are strings and values are scalars. One can define a ''hash'' to not coerce all keys to strings automatically: 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:
 
<syntaxhighlight lang=Perl6>
Line 1,282:
</syntaxhighlight>
 
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:
Line 1,292:
</syntaxhighlight>
 
By default, when iterating overthrough a ''hash'', one gets key/valuekey–value ''Pair''spairs.
 
<syntaxhighlight lang=Perl6>
Line 1,300:
</syntaxhighlight>
 
It's is also possible to get alternating key values and value values by using the ''<code>kv''</code> method:
 
<syntaxhighlight lang=Perl6>
Line 1,308:
</syntaxhighlight>
 
[[Raku (programming language)|Raku]] doesn't have any references. Hashes can be passed around as single parameters that doare not get flattened. If you want to make sure that a subroutine only accepts hashes, use the ''%'' sigil in the ''Signature''.
 
<syntaxhighlight lang=Perl6>
Line 1,319:
</syntaxhighlight>
 
CompliantIn compliance with the sense of [[gradual typing]], hashes may be subjected to type constraints, confining thea set of valid keys to a certain type.
 
<syntaxhighlight lang=Perl6>