Perl Programming Page 4 - Perl: More on Data Types and Operators |
If you want to add or subtract 1 from your value, you can use the incremental/decremental operators. Observe the code below: $pay_rate = 20; $pay_rate++; This would set your pay rate to 21. If you had used -- it would have lowered it to 19. Another thing to consider is the placement of your ++ symbol. Since it is after the variable, you use it differently from the way you'd use it if it was before the variable. Look at these two examples: $pay_rate = 20; $total_pay_rate = ++$pay_rate + 80; The above code would result in the pay rate being: 101. This is because the operator adds to the $pay_rate variable prior to the calculation. If we wrote it this way: $pay_rate = 20; $total_pay_rate = $pay_rate++ + 80; The result would be 100 because we increment the value of $pay_rate after the calculation. If you printed the $pay_rate and $total_pay_rate your results would be: $total_pay_rate: 100 $pay_rate: 21 Adding a String
If we want to add a string to an existing variable you could so by concatenating it. $my_sentence = 'This ' . 'is ' . 'my ' . 'sentence!'; If we printed that variable, we would get this result: This is my sentence! We could do the same thing to add variables to each other: $first_name = 'James '; $last_name = 'Payne'; $full_name = $first_name . $last_name; The $full_name variable would now contain the data: James Payne. Comparing Strings
In addition to adding strings to one another, you can compare them to each other as well. The easiest of this group of operators is the eq operator. It says, literally, this string = that string. The other operators work in this manner: lowercase letters are greater than uppercase letters, and a is greater than b (b is greater than c, etc). Here it is in code form: $name_one = 'James'; $name_two = 'Johnny'; if ($name_one gt $name_two) { print “James is greater than Johnny!”; } That code tells the program that if James is greater than Johnny (and it is), print this sentence.
blog comments powered by Disqus |
|
|
|
|
|
|
|