Search for substrings:
Index () and rindex ()
These two functions are used to find strings in strings. The rindex function is the same as the index function, except that it is located later.
There are two main usage options: Index ($ string, $ substring );
Index ($ string, $ substring, $ skip );
The $ skip parameter indicates the number of characters to skip. It is used to specify the start position. For the rindex () function, it is calculated from the end of the string.
Substr ()
Parameter: substr ($ string, $ start, $ length );
The usage of this function is a bit similar to that of C ++, but here the start position can be a negative number, which is a good feature. A negative number indicates that the count starts from the end of the string.
In addition, you can assign values to the result of the function, which modifies the corresponding substring in the string. It is also a good function, a bit similar to the replace function.
Sprintf ();
It is used to format a string in the same usage as sprintf in C.
About sorting:
By default, the sort function is sorted by ASCII code. When we sort a bunch of numbers, we do not want to sort them by ASCII code. This is a custom sorting rule.
-
- Sub by_number {
- If($<$ B){
-
- Return-1;
-
- } Elsif ($=$ B){
-
- Return0;
-
- } Elsif ($>$ B){
-
- Return1;
-
- }
- }
Then we can use
@ Sortresut = sort by_number @ Nums; the correct result is displayed.
For the above-, 1 relationship, you can use a simpler method,
- Sub by_number {
- $ A <=> $ B;
- }
Of course, you do not have to define a function. When your sorting rules are relatively simple, you can directly write your own rules behind the sort function.
For example
- @ Sortresult = sort {$ A <=> $ B} @ Nums;
You can use CMP to compare strings.
For example
- Sort {$ a cmp $ B} @ Nums;
The flexible formulation of sorting rules is very convenient. If the pointer of the function passed in C ++ is used to call back the function.
Write it here, not to be continued;