Perl Study Notes 14 string and sorting

Source: Internet
Author: User

1. Search by index in a string

$ Where = index ($ words, $ word );# Find the place where word appears for the first time from words. The subscript starts from 0. If the search fails,-1 is returned;

Specify where to start searching: $ where = index ($ words, $ word, 10 );# Search from 10 onwards, including 10

The final position of the substring to be searched:

Specify the maximum returned position: $ where = rindex ($ words, $ word, 10 );# Only search for characters whose subscript is 10 or earlier.

2. Process substrings

Substr Parameters: String, start subscript, and length to be intercepted (the actual length will prevail if the actual length is exceeded)

If the third parameter is omitted, it is always taken to the end of the string:

StringThe start position can be a negative value, indicating the reciprocal from the end of the string:

SubSTR can be used together with index, which is more efficient than regular expressions:

Assume thatA string is a variable. You can modify the selected part of the string. The length of the substring does not need to be the same as that of the replaced string:

UseThe binding operator operates on a part of a string:

In additionA replacement method. The return value is the substring before replacement:

3Format data with sprintf

The result is the same as that of printf, but the processed string is returned without printing:

ProcessingFinancial data example:

TheIILineIt is mainly used to execute conditional expressions, while the cyclic subject is only a placeholder and can be replaced:

4Advanced sorting

First, create your own sorting Subroutine:

Then, Call:

UFOThe operator (<=>) compares two numbers and returns 0, 1,-1:

CMPOperator to compare the string size:

EmbeddedSubroutine:

DecreaseOrdered sorting:

5,Hash sort by value:

PressMulti-value sorting:



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.