Perl 6 screencast - part 5 - hashes

This article is about Perl 6. If you are looking for solution regarding the current production version of Perl 5, please check out the Perl tutorial.

Direct link to the Perl 6 screencast about hashes

See more Perl 6 entries.

Perl 6 Code examples

Hashes in Perl 6 are denoted using % sign: Creating a hash

  my %h = "Foo" => 1, "Bar" => 2;

printing it out for debugging purposes:

  say %h.perl;    # {"Foo" => 1, "Bar" => 2}

printing the value of a single key:

  say %h{"Foo"};  # 1

The quotation marks are required around the string:

  say %h{Foo};    # Could not find sub &Foo

You can use variables without quoting them:

  my $name = "Foo";
  say %h{$name};   # 1

Recall from the previous section that you can use the angle brackets to quote a number of strings and create a list of them:

  say <Foo Bar>;   # FooBar

This notation can be used to eliminate the need for quotation marks:

  say %h<Foo>;     # 1

You can actually use multiple keys there fetching a list of values of a hash slice:

  say %h<Foo Bar>;  # 1 2

Using the "perl" method just further proves that you get back a list of values

  say %h<Foo Bar>.perl;  # (1, 2)

In order to fetch all the keys you can use the ".keys" method and then you can iterate over the individual keys and fetch the respective values:

  for %h.keys -> $k { say "$k %h{$k}"; }

There is also a ".values" method that will return the list of values. Even though in our case the values are unique in the general case you cannot easily get back the keys from the values:

  for %h.values -> $v { say $v; }

In case you prefer to iterate over the key-value pairs for that you can use the ".kv" method:

  for %h.kv -> $k, $v { say "$k - $v"; }

To add another key with its value you can use the following code:

  %h<Moo> = 3;

  say %h.perl;    # {"Moo" => 3, "Foo" => 1, "Bar" => 2}

If the key already exists this will replace the value in the hash:

  %h<Moo> = 4;
  say %h.perl;   # {"Moo" => 4, "Foo" => 1, "Bar" => 2}

On the other hand in Perl 6 you can also use the ".push" method on a hash that will add the value to the given key creating an array as the value of that key:

  %h.push( 'Moo' => 5 );
  say %h.perl;   # {"Moo" => [4, 5], "Foo" => 1, "Bar" => 2}

Published on 2010-07-31 by Gabor Szabo

In the comments, please wrap your code snippets within <pre> </pre> tags and use spaces for indentation.
blog comments powered by Disqus
Python, JavaScript, Node.js, Ruby, and more.

Google Plus Twitter RSS feed