当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Perl print用法及代码示例


打印运算符珀尔用于打印作为参数传递给它的列表中表达式的值。打印运算符打印作为参数传递给它的任何内容,无论它是字符串、数字、变量还是其他内容。 Double-quotes(“”) 用作该运算符的分隔符。

用法: print “”; or print(); or print(” “);
返回: 
0 on failure and 1 on success.

示例 1:

珀尔


#!/usr/bin/perl -w
# Defining a string
$string = "Geeks For Geeks";
# Defining an array of Integers
@array = (10, 20, 30, 40, 50, 60);
# Searching a pattern in the string
# using index() function
$index = index ($string, 'or');
# Printing the position of matched pattern
print "Position of 'or' in the string $index\n";
# Printing the defined array
print "Array of Integers is @array\n";
输出:
Position of 'or' in the string 7
Array of Integers is 10 20 30 40 50 60

示例 2:

珀尔


#!/usr/bin/perl -w
# Defining a string
$string = "Welcome to GFG";
# Defining an array of integers
@array = (-10, 20, 15, -40, 45, -60);
# Searching a pattern in the string
# using index() function
$index = index($string, 'o G');
# Printing the position of matched pattern
print "Position of 'o G' in the string $index\n";
# Printing the defined array
print "Array of Integers @array\n";
输出:
Position of 'o G' in the string 9
Array of Integers -10 20 15 -40 45 -60

我们还可以使用像 print() 这样带大括号的 print 来打印一些东西,它的作用与上面相同,这是针对那些来自其他语言的开发人员,其中有 print() 而不是打印“”。

珀尔


#!/usr/bin/perl
# your code here
#!/usr/bin/perl -w
# Defining a string
$string = "Welcome to GFG";
# Defining an array of integers
@array = (-10, 20, 15, -40, 45, -60);
# Searching a pattern in the string
# using index() function
$index = index($string, 'o G');
# Printing the position of matched pattern
print("Position of 'o G' in the string $index\n");
# Printing the defined array
print("Array of Integers @array\n");

输出 -

Time Complexity - O(1).
Space Complexity - O(1).


相关用法


注:本文由纯净天空筛选整理自Code_Mech大神的英文原创作品 Perl | print operator。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。