📜  珀尔 |标量关键字

📅  最后修改于: 2022-05-13 01:54:52.077000             🧑  作者: Mango

珀尔 |标量关键字

Perl 中的 scalar 关键字用于将表达式转换为标量上下文。这是对标量上下文的表达式的有力评估,即使它在列表上下文中运行良好。

示例 1:

Perl
#!/usr/bin/perl -w
 
# Defining Arrays
@array1 = ("Geeks", "For", "Geeks");
@array2 = (1, 1, 0, 0, 9, 6);
 
# Concatenation of both arrays
@array3 = (@array1, @array2);
 
# Printing the Concatenated Array
# in List form
print "Array in List form: @array3\n";
 
# Conversion of Arrays to scalar context
@array3 = (scalar(@array1), scalar(@array2));
 
# Conversion to scalar returns
# the number of elements in the array
print "Array in scalar form: @array3\n";


Perl
#!/usr/bin/perl -w
 
# Defining Arrays
@array1 = ("Welcome", "To", "Geeks");
@array2 = (1, 1, 0, 0, 9, 6);
 
# concatenation of both arrays
@array3 = ( @array1, @array2 );
 
# Printing the Concatenated Array
print "Concatenation of Arrays: @array3\n";
 
# Conversion of Arrays to scalar context to
# Evaluate Difference between size of arrays
@array3 = (scalar(@array2) - scalar(@array1));
 
# Printing the size Difference
print "Difference in number of elements: @array3\n";


输出:
Array in List form: Geeks For Geeks 1 1 0 0 9 6
Array in scalar form: 3 6

示例 2:

Perl

#!/usr/bin/perl -w
 
# Defining Arrays
@array1 = ("Welcome", "To", "Geeks");
@array2 = (1, 1, 0, 0, 9, 6);
 
# concatenation of both arrays
@array3 = ( @array1, @array2 );
 
# Printing the Concatenated Array
print "Concatenation of Arrays: @array3\n";
 
# Conversion of Arrays to scalar context to
# Evaluate Difference between size of arrays
@array3 = (scalar(@array2) - scalar(@array1));
 
# Printing the size Difference
print "Difference in number of elements: @array3\n";
输出:
Concatenation of Arrays: Welcome To Geeks 1 1 0 0 9 6
Difference in number of elements: 3