Some functions about strings in Perl

Source: Internet
Author: User
Tags call back vars

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.

  1. Sub by_number {
  2. If($<$ B){
  3. Return-1;
  4. } Elsif ($=$ B){
  5. Return0;
  6. } Elsif ($>$ B){
  7. Return1;
  8. }
  9. }

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,

    1. Sub by_number {
    2. $ A <=> $ B;
    3. }

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

    1. @ Sortresult = sort {$ A <=> $ B} @ Nums;

You can use CMP to compare strings.

For example

    1. 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;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.