當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。