-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlearning_perl.pl
80 lines (61 loc) · 1.37 KB
/
learning_perl.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/perl
# Operators and precedence.
$sum = (4 +4) / 4;
print "$sum\n";
$number = 10;
$number++;
print "$number\n";
$number = 10;
--$number;
print "$number\n";
$letter = "A";
$letter++;
print "$letter\n";
$sum = 2 ** 3;
print "$sum\n";
$sum = 10 % 3;
print "$sum\n";
# The output of the belwo will be 3.33...
## We will use integer to ignore the decimal place.
use integer;
$sum = 10 / 3;
print "$sum\n";
# Now the eqality operators.
# notice how the strings are used with eq as an operator whereas in number the operator is ==
$number = 10;
if ($number == 10) {
print "10\n";
}
$word = "hello";
if ($word eq "hello") {
print "Hello\n";
}
# Inequality operators.
# Notice the operators used in each case
$number = 190;
if ($number != 10) {
print "Not 10\n";
}
$word = "Hello";
if ($word ne "hello") {
print "Not Hello\n";
}
# While loop
$count = 1;
while ($count <= 10 ) {
print "Hello\n";
$count++;
}
@array = qw(frog dog bird cat elephant);
$count = 0; # arrays are indexed from zeros.
while ($count <= 4) {
print "$array[$count]\n";
$count++;
}
# Until loop, it is reverse to the while loop, hence meaning until the condition is true, keep doing this.
@array = qw(frog dog bird cat elephant);
$count = 0;
until (($array[$count] eq "bird")) {
print "$array[$count]\n";
$count++;
}