7/16/2023 0 Comments Perl string comparisonSee the sorting howto and the sorting tutorial. There are two of these: cmp for strings and the spaceship operator for numbers. When defining your own sort function, you will need to use the comparison operators. # x = allow the regexp to go over multiple lines My $string1 = 'Jimmy has 17 dollars and 20 cents saved up' Īnd the next example shows how to check if a string contains a phrase like: '4 dollars and 25 cents': The first example checks that a string does not contain any numbers: However, the following examples should give you a basic idea. Regular expressions are covered in great detail in perldoc perlre and perldoc perlretut, and also on this site at Regular Expressions. For example, you may want to know if a string contains a number, or if it starts with a certain word. Much more powerful comparisons are possible using Perl's Regular Expressions, which allow you to compare some input with a pattern. Print "Greater than or level with llama\n" The eq operator is used in PERL to find out whether two string values are equal or not. The following example compares $string1 to different strings using the different methods: These will compare the strings alphabetically or 'stringwise'. If you want to compare strings to determine which comes first alphabetically, you can use the lt and gt operators, or the le and ge operators for less/greater than equals comparisons. If you try to compare numbers with eq, they will be converted to strings: Here is an example of the eq operator:Ī similar example but this time using the ne operator: These work much the same as the = and != operators, but they expect the variables to be strings. To compare two strings use the eq and ne operators. Print "Greater than or equal to four!\n" The following example compares $num1 to different numbers using different operators: If you want to check if a number is less than or greater than another number, use the less/greater than ( ) operators, or the less/greater than equals operators ( =). It is not recommended that you rely on Perl evaluating a string containing numbers and letters as a number. Whereas, in the following example the string would equate to 12, although you'd still get a warning:Īrgument "12aa" isn't numeric in numeric eq (=) at. line 8.Īrgument "three" isn't numeric in numeric eq (=) at. If the string starts with numbers, Perl will use these, otherwise the string equates to 0.įrom the above example, you would get warning messages and both strings would evaluate to zero:Īrgument "five" isn't numeric in numeric eq (=) at. However, Perl will still attempt to convert the string into a number. If you want a case insensitive string comparison, use the lc function to convert the strings to lowercase beforehand. If you try to compare strings using =, and you have included the use warnings pragma, you will get a warning, as in the example below. Perls string comparison is case-sensitive. This numeric not-equal != operator allows you to test for inequality: To compare numbers for equality in Perl, use the = operator: String comparison and numeric comparison are done separately, with one operator for strings and another for numbers. This page shows different ways to compare scalar values in Perl.
0 Comments
Leave a Reply. |