1. Perl
  2. reading material

Is the hash variable name singular or plural?

Scalar variable can be singular and a array variable can be plural, but in the case of a hash variable, I'm always wondering whether the variable name should be singular or plural. Let's think about the policy of how to put it on.

Scalar variable are always singular.

my $name = 'kimoto';

Array variable are always plural.

my @names = ('kimoto', 'tanaka');

What about a hash variable? Then, when expressing the information of the book first. There is only one book. The book contains information that represents the book. In such cases, I feel that the singular is better.

my %book = {
  id => '00000001',
  title =>'Perl'
}
my $id = $book{id};
my $title = $book{title};

Next, let's consider the case of specifying a name and retrieving the father's name. It's the same type and contains information about multiple people. In such cases, I feel that the plural is better.

my %fathers = (
  ken =>'taro',
  naoto =>'ichiro'
);
my $father = $fathers{ken};

In other words, in the case of hashes, there are cases where it is more natural to make the variable name singular, and there are cases where it is more natural to make it plural.

Related Informatrion