Saturday, November 23, 2019
A Beginner Guide to Comparing Values in Perl
A Beginner Guide to Comparing Values in Perl Perlà comparison operators can sometimes be confusing to new Perl programmers. The confusion stems from the fact thatà Perlà actually has two sets of comparison operators - one for comparing numeric values and one for comparing string American Standard Code for Information Interchange (ASCII) values.à Sinceà comparison operatorsà are typically used to control logical program flow and make important decisions, using the wrong operator for the value you are testing can lead to bizarre errors and hours of debugging, if youre not careful. Dont forget to catch whats written at the very bottom of this page for some last-minute things to remember. Equal, Not Equal The simplest and probably most used comparison operators test to see if one value is equal to another value. If the values are equal, the test returns true, and if the values are not equal, the test returns false. For testing the equality of two numeric values, we use the comparison operator . For testing the equality of two string values, we use the comparison operator eq (EQual). Heres an example of both: if (5 5) { print for numeric values\n; } if (moe eq moe) { print eq (EQual) for string values\n; } Testing for the opposite, not equal, is very similar. Remember that this test will return true if the values tested are not equal to each other. To see if two numeric values are not equal to each other, we use the comparison operator !. To see if two string values are not equal to each other, we use the comparison operator ne (Not Equal). if (5 ! 6) { print ! for numeric values\n; } if (moe ne curly) { print ne (Not Equal) for string values\n; } Greater Than, Greater Than or Equal To Now lets look at theà greater thanà comparison operators. Using this first operator, you can test to see if one value isà greater thanà another value. To see if twoà numericà values areà greater thanà each other, we use the comparison operatorà . To see if twoà stringà values areà greater thanà each other, we use the comparison operatorà gtà (Greater Than). if (5 4) { print for numeric values\n; } if (B gt A) { print gt (Greater Than) for string values\n; } You can also test forà greater than or equal to, which looks very similar. Keep in mind that this test will returnà trueà if the values tested are equal to each other, or if the value on the left is greater than the value on the right. To see if twoà numericà values areà greater than or equal toà each other, we use the comparison operatorà . To see if twoà stringà values areà greater than or equal toà each other, we use the comparison operatorà geà (Greater-than Equal-to). if (5 5) { print for numeric values\n; } if (B ge A) { print ge (Greater-than Equal-to) for string values\n; } Less Than, Less Than or Equal To There are a variety of comparison operators you can use to determine the logical flow of yourà Perl programs. Weve already discussed the difference between the Perl numeric comparison operators and the Perl string comparison operators, which can cause some confusion toà new Perl programmers.à Weve also learned how to tell if two values are equal to, or not equal to each other, and weve learned how to tell if two values are greater than or equal to each other. Lets look at theà less thanà comparison operators. Using this first operator, you can test to see if one value isà less thanà another value. To see if twoà numericà values areà less thanà each other, we use the comparison operatorà . To see if twoà stringà values areà less thanà each other, we use the comparison operatorà ltà (Less Than). if (4 5) { print for numeric values\n; } if (A lt B) { print lt (Less Than) for string values\n; } You can also test for,à less than or equal to, which looks very similar. Remember that this test will returnà trueà if the values tested are equal to each other, or if the value on the left is less than the value on the right. To see if twoà numericà values areà less than or equal toà each other, we use the comparison operatorà . To see if twoà stringà values areà less than or equal toà each other, we use the comparison operatorà leà (Less-than Equal-to). if (5 5) { print for numeric values\n; } if (A le B) { print le (Less-than Equal-to) for string values\n; } More Information on Comparison Operators When we talk about string values being equal to each other, were referring to their ASCII values. So, the capital letters are technically less than the lowercase letters, and the higher the letter is in the alphabet, the higher the ASCII value. Make sure you check yourà ASCII valuesà if youre trying to make logical decisions based on strings.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.