1

I'm totally new in perl. I want to get element's value by its key from an associative array. my array is:

my %array = a.a.a.a => "my name",
       b.b.b.b => "my home",
       c.c.c.c => "my city";

when I print

print say %array<b.b.b.b>;
or 
print say %array{b.b.b.b};

it shows error, so how can I get this? code-test link: codepad link

fatih
  • 330
  • 8
  • 17
  • is there any problem in my array? – fatih Sep 04 '15 at 10:34
  • 4
    You are not using Perl syntax. Also, instead of saying "it shows error", you should *show the error* so we know what it is. – TLP Sep 04 '15 at 11:44
  • 3
    List of mistakes: 1) You must enclose list in parentheses when assigning to a hash or array, 2) You must (in this case) quote the keys, because `.` is an operator, 3) `print say` is doing the same thing twice. It will also print a `1` (the return value from `say`), 4) Angle braces `<>` are used for globs or file handles, never for hashes. – TLP Sep 04 '15 at 11:50
  • 1
    @TLP I think OP is writing Perl6 (see my answer), in which case it only has one mistake. If not, then the syntax is wrong, but I would be amazed if these are random mistakes that follow Perl6 rules. – SES Sep 04 '15 at 17:38

4 Answers4

2

Associate arrays are called Hash in Perl.

Always use use strict; use warnings; in your Perl code. If you use it you will get to know that keys in your hash are not quoted.

#!/usr/bin/perl
use strict;
use warnings;

my %hash = (
        'a.a.a.a' => "my name",
        'b.b.b.b' => "my home",
        'c.c.c.c' => "my city"
    );

To access value of a key you do $hash{$key}, so to access b.b.b.b

print $hash{'b.b.b.b'};

Demo

Chankey Pathak
  • 21,187
  • 12
  • 85
  • 133
  • 1
    You are welcome. You may want to read [article on hashes by Gabor](http://perlmaven.com/perl-hashes). – Chankey Pathak Sep 04 '15 at 10:41
  • No need to quote the keys *unless* using strict/warnings. – RobEarl Sep 04 '15 at 10:50
  • 1
    It's best practice to `use strict/warnings`. You should read [Why use strict and warnings?](http://stackoverflow.com/questions/8023959/why-use-strict-and-warnings) – Chankey Pathak Sep 04 '15 at 10:54
  • Your answer essentially says "Do X, then fix Y" except doing X caused Y. All the actual problems (hash declaration, hash access, print/say) have no explanation as if they're incidental. – RobEarl Sep 04 '15 at 11:06
  • 4
    @RobEarl If you do not quote the keys in this case, it will be considered an expression rather than a string, meaning that `a.a.a.a` will become `aaaa` (4 `a` concatenated by `.`), and you will get a bareword warning. Strict and warnings never cause problems, they only tell you that problems exist. – TLP Sep 04 '15 at 11:47
  • @TLP +1 for your detail explanation. – fatih Sep 04 '15 at 12:26
  • @TLP So X didn't exactly *cause* Y, but without X or Y the code would've run without error (though differently to intended!). I'm not arguing against strict/warnings, I'm arguing against "quote the hash keys" as a good answer, at least without any further explanation as to why that's required in this instance. – RobEarl Sep 04 '15 at 12:43
2

What you wrote is almost valid Perl6, with the only mistake being not quoting the keys. And that is only necessary in this example because .a and .b look like a method call in Perl6 and will generate undeclared subroutine warnings.

my %array =
   'a.a.a.a' => "my name",
   'b.b.b.b' => "my home",  
   'c.c.c.c' => "my city";

say %array<b.b.b.b>;
say %array{'b.b.b.b'};

Running this gives what you would expect:

$ perl6 hash.pl6
my home
my home

This example code looks more like Perl6 than Perl 5 to me, so I thought I would mention this for reference in case you were following a Perl6 tutorial and trying to compile the code with perl.

SES
  • 850
  • 1
  • 9
  • 21
1

use :

my %array = ("a.a.a.a" => "my name",
       "b.b.b.b" => "my home",
       "c.c.c.c" => "my city");

print $array{"b.b.b.b"};
Jens
  • 67,715
  • 15
  • 98
  • 113
0

use

print say $array{"b.b.b.b"};

or

print "$array{'b.b.b.b'}\n";
newcoder
  • 464
  • 9
  • 23