标量在perl中是什么意思?
What does scalar mean in perl?
在 perl 中,您有三个主要 data types,“标量、标量数组和关联标量数组”。 perl 究竟想用“标量”这个名字表达什么?什么是隐喻,应该形成的心理形象?
我可以推断出一个类比:单个值、值列表、命名值列表。但这并不能帮助我理解为什么使用“标量”这个词。 Perl 的标量肯定不会 resemble a ladder.
Variable (Computer Science) 原则上描述了这个概念,其中说:
In computer programming, a variable or scalar is a storage location
(identified by a memory address) paired with an associated symbolic
name, which contains some known or unknown quantity of information
referred to as a value. The variable name is the usual way to
reference the stored value, in addition to referring to the variable
itself, depending on the context.
在 Perl 中,标量变量保存单个值。虽然 Perl 标量通常与符号名称相关联,但并非必须如此;您可以拥有一个仅由标量引用引用的标量,而不与符号配对。计算机编程中“标量”一词的用法可能源自数学中的类似用法,如 Scalar (Mathematics):
中所述
A scalar is an element of a field which is used to define a vector
space. A quantity described by multiple scalars, such as having both
direction and magnitude, is called a vector.
应用于 Perl 的标量在 perldoc perldata:
中描述
A scalar may contain one single value in any of three different
flavors: a number, a string, or a reference. In general, conversion
from one form to another is transparent. Although a scalar may not
directly hold multiple values, it may contain a reference to an array
or hash which in turn contains multiple values.
Perl 标量,在内部存储一些东西; IV(整数值),NV(浮点数值),PV(指向字符串值的指针),SV(引用),引用计数,一些用于跟踪标量是否具有 Unicode 语义的各种标志,其他“ magic”,以及正在使用的各种存储类型中的哪一种。并非所有这些桶在任何给定时间都在使用,填充哪些桶取决于标量的定义方式和使用方式(持有字符串的标量稍后可能会在内部填充 IV 或 NV 字段,如果它被视为一个数字)。 perlguts中对此进行了描述,但您通常无需担心。
您可以非正式地将标量视为单个存储单元,其中可能包含数字、字符串或引用。您可以将标量视为独立变量、标量引用引用的存储单元,或者数组或散列的单个元素。
标量是单个不同的值,例如 47 或“Bob Smith”。标量变量使用 $
标记。
my $score = 47;
my $name = "Bob Smith";
数组是 0 个或多个标量的有序列表,可由非负整数索引。数组变量使用 @
标记。
my @scores = ( 47, 52, 34 );
my @names = ( "Bob Smith", "Susan Richardson", "Heather Jones" );
my $first_score = $scores[0]; # 47
my $last_score = $scores[-1]; # 34, indexed from the end
散列或关联数组是映射到标量的字符串的无序列表。哈希变量使用 %
印记。
my %scores = (
"Bob Smith" => 47,
"Susan Richardson" => 52,
"Heather Jones" => 34,
);
my $score_for_susan = $scores{"Susan Richardson"}; # Has the value 52
my $score_for_doug = $scores{"Doug Thompson"}; # Has the value undef
# because the key "Doug Thompson" does not exist in %scores.
查找值时,像 %scores
这样的散列表示为 $scores
可能会造成混淆。
在 perl 中,您有三个主要 data types,“标量、标量数组和关联标量数组”。 perl 究竟想用“标量”这个名字表达什么?什么是隐喻,应该形成的心理形象?
我可以推断出一个类比:单个值、值列表、命名值列表。但这并不能帮助我理解为什么使用“标量”这个词。 Perl 的标量肯定不会 resemble a ladder.
Variable (Computer Science) 原则上描述了这个概念,其中说:
In computer programming, a variable or scalar is a storage location (identified by a memory address) paired with an associated symbolic name, which contains some known or unknown quantity of information referred to as a value. The variable name is the usual way to reference the stored value, in addition to referring to the variable itself, depending on the context.
在 Perl 中,标量变量保存单个值。虽然 Perl 标量通常与符号名称相关联,但并非必须如此;您可以拥有一个仅由标量引用引用的标量,而不与符号配对。计算机编程中“标量”一词的用法可能源自数学中的类似用法,如 Scalar (Mathematics):
中所述A scalar is an element of a field which is used to define a vector space. A quantity described by multiple scalars, such as having both direction and magnitude, is called a vector.
应用于 Perl 的标量在 perldoc perldata:
中描述A scalar may contain one single value in any of three different flavors: a number, a string, or a reference. In general, conversion from one form to another is transparent. Although a scalar may not directly hold multiple values, it may contain a reference to an array or hash which in turn contains multiple values.
Perl 标量,在内部存储一些东西; IV(整数值),NV(浮点数值),PV(指向字符串值的指针),SV(引用),引用计数,一些用于跟踪标量是否具有 Unicode 语义的各种标志,其他“ magic”,以及正在使用的各种存储类型中的哪一种。并非所有这些桶在任何给定时间都在使用,填充哪些桶取决于标量的定义方式和使用方式(持有字符串的标量稍后可能会在内部填充 IV 或 NV 字段,如果它被视为一个数字)。 perlguts中对此进行了描述,但您通常无需担心。
您可以非正式地将标量视为单个存储单元,其中可能包含数字、字符串或引用。您可以将标量视为独立变量、标量引用引用的存储单元,或者数组或散列的单个元素。
标量是单个不同的值,例如 47 或“Bob Smith”。标量变量使用 $
标记。
my $score = 47;
my $name = "Bob Smith";
数组是 0 个或多个标量的有序列表,可由非负整数索引。数组变量使用 @
标记。
my @scores = ( 47, 52, 34 );
my @names = ( "Bob Smith", "Susan Richardson", "Heather Jones" );
my $first_score = $scores[0]; # 47
my $last_score = $scores[-1]; # 34, indexed from the end
散列或关联数组是映射到标量的字符串的无序列表。哈希变量使用 %
印记。
my %scores = (
"Bob Smith" => 47,
"Susan Richardson" => 52,
"Heather Jones" => 34,
);
my $score_for_susan = $scores{"Susan Richardson"}; # Has the value 52
my $score_for_doug = $scores{"Doug Thompson"}; # Has the value undef
# because the key "Doug Thompson" does not exist in %scores.
查找值时,像 %scores
这样的散列表示为 $scores
可能会造成混淆。