We need not to specify the type of data in Perl. Perl interpreter choose it automatically based on the context of data.
Perl has three data types:
1) Scalars:
Integer Example:
Output:
5
v) This function will get two strings and return the location of the second string within the first string.
2) Arrays:
Output: $names[0] = Vinod
3) Hashes:
Output: $data{'Vinod'} = 25
Perl has three data types:
1) Scalars:
- Scalars are single data unit.
- A scalar can be integer, float, string etc. Scalar variables are prefixed with “$” sign.
Perl supports following operators on scalars:
- Concatenation of string values via the . (dot) operator.
- Math functions on numeric values: + - * / % ^^ as well as a rich set of functions.
- Operations on own variable: += -= .= ++ -- etc.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | #!/usr/bin/perl use strict; use warnings; #scalar data could be integer or float my $v = -2; my $w = 2; my $x = 5; my $y = 10.50; my $z = 20; print "X = $x\n"; print "Y = $y\n"; print "Z = $z\n"; print $x + $y . "\n"; print $y * $z, "\n"; print $x . $w, "\n"; my $p = "5"; my $q = "2cm"; print print $p . $q, "\n"; |
5
10.5
20
15.5
210
52
52cm
String Example:
20
15.5
210
52
52cm
String Example:
i) Print String Variables
Output:
Hello, Vinod
Hello, $name\n
ii) Print String Variables
Output:
The total cost for 5 Apples will be: 67.5 Dollars.
The total cost for 5 Apples will be: 67.5 Dollars.
iii) Alternative delimiters
Besides the single and double-quotes, Perl also allows you to use quote-like operators such as:
a. The q// acts like single-quoted string.
b. The qq// acts like double-quoted string.
Output:
"Are you learning Perl String today?" We asked.
$size=15;
$name = "Vinod";
print "Hello, $name\n"; # prints Hello, Vinod
print 'Hello, $name\n'; # prints Hello, $name\n
Hello, Vinod
Hello, $name\n
ii) Print String Variables
1 2 3 4 5 6 7 8 9 10 | # scalar variable example $item_name = "Apple"; $item_price = 13.50; $item_count = 5; print "The total cost for $item_count $item_name" . "s" . " will be: " . $item_count * $item_price . " Dollars.\n"; # Alternate way $total = $item_count * $item_price; $item_name_plural = $item_name . "s"; print "The total cost for $item_count $item_name_plural will be: $total Dollars.\n"; |
The total cost for 5 Apples will be: 67.5 Dollars.
The total cost for 5 Apples will be: 67.5 Dollars.
iii) Alternative delimiters
Besides the single and double-quotes, Perl also allows you to use quote-like operators such as:
a. The q// acts like single-quoted string.
b. The qq// acts like double-quoted string.
1 2 3 4 5 6 7 8 9 10 | #!/usr/bin/perl use warnings; use strict; my $s= q/"Are you learning Perl String today?" We asked./; print($s ,"\n"); my $name = 'Jack'; my $s2 = qq/"Are you learning Perl String today?"$name asked./; print($s2 ,"\n"); |
"Are you learning Perl String today?" We asked.
"Are you learning Perl String today?"Jack asked.
iv) String functions
iv) String functions
1 2 3 4 5 6 7 8 9 10 11 12 | #!/usr/bin/perl my $s = "This is a sample string"; print("To upper case:\n"); print(uc($s),"\n"); print("To lower case:\n"); print(lc($s),"\n"); my $sub = "sample"; my $p = index($s,$sub); # rindex($s,$sub); print(qq\The substring "$sub" found at position "$p" in string "$s"\,"\n"); |
Output:
To upper case:
To upper case:
THIS IS A SAMPLE STRING
To lower case:
this is a sample string
The substring "sample" found at position "10" in string "This is a sample string"
To lower case:
this is a sample string
The substring "sample" found at position "10" in string "This is a sample string"
v) This function will get two strings and return the location of the second string within the first string.
1 2 3 4 5 6 7 8 9 | use strict; use warnings; my $str = "The black cat climbed the green tree"; print "Index of cat: ", index $str, 'cat'; print "\nIndex of dog: ", index $str, 'dog'; print "\nIndex of The: ", index $str, "The"; print "\nIndex of the: ", index $str, "the"; |
Output:
Index of cat: 10
Index of dog: -1
Index of The: 0
Index of the: 22
vi)Perl String Escaping Characters
Output:
javatpoint@gmail.com
We have defined $msg1 as GoodMorning
The \n is a new line.
This "blogspot" is "Perl" tutorials
vii) q and qq operator
a. The qq operator replaces double quote surrounding a string by its parentheses. You can use qq instead of ("").
b. The q operator replaces single quote surrounding a string by its parentheses. You can use q instead of (').
Output:
This is sample text
The value of $str is "World is beautiful.
The value of \$str1 is \"$str1\. \n"
vii)What is chop() and chomp() function in Perl?
Output:
AEIO
U
AEIOU 1
Index of cat: 10
Index of dog: -1
Index of The: 0
Index of the: 22
vi)Perl String Escaping Characters
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | use strict; use warnings; #Printing E-mail Address my $site = "javatpoint\@gmail.com"; print "$site\n"; #prints javatpoint@gmail.com #Perl $ sign Embedding in Double quote String my $msg1 = 'GoodMorning'; print "We have defined \$msg1 as $msg1\n"; #Prints We have defined $msg1 as GoodMorning #Escaping Escape Character print "The \\n is a new line.\n"; #print The \n is a new line. #Escaping Double quotes my $x = 'Perl'; print "This \"blogspot\" is \"$x\" tutorials\n"; |
javatpoint@gmail.com
We have defined $msg1 as GoodMorning
The \n is a new line.
This "blogspot" is "Perl" tutorials
vii) q and qq operator
a. The qq operator replaces double quote surrounding a string by its parentheses. You can use qq instead of ("").
b. The q operator replaces single quote surrounding a string by its parentheses. You can use q instead of (').
1 2 3 4 5 6 7 8 9 | use strict; use warnings; my $str = "World is beautiful"; print qq(This is sample text\n); print qq(The value of \$str is \"$str\. \n"); my $str1 = "World is beautiful"; print q(The value of \$str1 is \"$str1\. \n"); |
This is sample text
The value of $str is "World is beautiful.
The value of \$str1 is \"$str1\. \n"
vii)What is chop() and chomp() function in Perl?
- Perl chop() function removes last character from a string regardless of what that character is. It returns the chopped character.
- Perl chomp() function removes any new line character from end of the string. It returns the number of characters removed from the string.
1 2 3 4 5 6 7 8 9 10 11 | #chop() function removes last character from a string $a = "AEIOU"; $b = chop($a); print "$a\n"; #it will return AEIO. print "$b\n"; #it will return U. #chomp() EXAMPLES $a = "AEIOU\n"; $b= chomp($a); print "$a $b"; #it will return AEIOU 1 |
AEIO
U
AEIOU 1
- Arrays are ordered list of scalars
- Array variables are prefixed with “@” sign
Example:
@names = ("Vinod", "Amol", "Yusuf", "Nilesh"); print "\$names[0] = $names[0]\n";
3) Hashes:
- Perl hashes are an unordered collection of key-value pairs.
- They are preceded by (%) sign and accessed using keys.
%data = ('Vinod', 25, 'Amol', 40, 'Yusuf', 30); print "\$data{'Vinod'} = $data{'Vinod'}\n";
This comment has been removed by a blog administrator.
ReplyDelete