In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the Perl string and sorting how to use, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.
Perl string and sort
1. Use the index to find substrings
The method of lookup in the Perl language depends on where it is found. Luckily, if you look in a large string, the index function can help you. Character positions are numbered from 0.
$where=index ($big,$small); find where the substring appears * times in a large string, return the position of * characters, and return-1 if the substring does not exist. You can think of the number of positions (positionnumber) as the character through which the substring was found.
My$where2=index ($stuff, "w", $where+1); # $where gets 6, and the third parameter gives the minimum value of the return value.
You may want to know where a substring appears. You can do this using the rindex function, which returns the position from the head of the string.
2. Substr operation substring in Perl language
Substr handles only part of the string. $part=substr ($string,$initial_position,$length)
A string, an initial position numbered from 0, and the length of the substring. The return value is a substring.
If the length of the request exceeds the length of the string, you get a shorter result than you want. If you want to explicitly ask to reach the end of the string, whether it is long or short, you can omit the third parameter.
The initial position can be negative, indicating that it starts at the end of the string (in this case,-1 represents * * one character)
The corresponding position selected in the string can be changed:
My$string= "Hello,world!"
Substr ($string,0,5) = "Goodbye"; # $string is now "Goodbye,world!"
Substr ($string,-20) = ~ s/fred/barney/g
Susbtr: use four parameters, and the fourth parameter is the replacement string:
My$previous_value=substr ($string,0,5, "Goodbye")
3. Sprintf in Perl language
The argument of the sprintf function is exactly the same as that of printf (except for the optional file handle), but it returns the requested string rather than printing it out. This is convenient for situations where you want to store a string in a format into a variable for future use.
My$data_tag=sprintf "% 4d/d/dd:d:d", $yr,$mo,$da,$h,$m,$s
$date_tag gets a value such as "2038Universe 01max 19300Rut08". The meaning of the leading zero in a formatted number is that, if necessary, precede it with a 0 to make it the desired width.
4 、 subbig_money
{
My$number=sprintf ".2f", shift@_
1while$number=~s/ ^ (-?\ d +) (\ d\ d\ d) / $1 in the do-nothing loop, add a comma at a time
$number=~s/ ^ (-?) / $1\ $/; # put the dollar sign in the right place
$number
}
5. High-level sorting in Perl language
The sorting subroutine returns a value indicating how the two elements are compared. If $an appears before $b in the final result, its sorting subroutine returns-1. If $b appears before $a, 1 is returned. If the order of $an and $b does not matter, the subroutine returns 0.
Subby_number {
If ($aqb) {1} else {0}
}
To use a sorting subroutine, place it (without using &) between the keyword sort and the list you want to sort.
Many of my@result=sortby_number@some_numbers; 's sorting subroutines begin with by_ to describe how to sort.
We do not need to declare $an and $b in the sorting subroutine and set them. If done, the subroutine will not get the correct result. We have Perl assign values to $an and $b for us, and we just need to write how to compare.
Three-way comparison numeric operator: the spaceship symbol (). This operator compares two numbers, sorts them by number, and returns-1j0pl 1. Subby_number {$aquib}
Three-way operator of a string: cmp
Subcase_insenstive {"\ Lingua" cmp "\ Lamplb"} case-insensitive sort
When your sorting subroutine is as simple as our one here, you can make it easier by using the "inline" subroutine instead of the sorting name, such as: my@numbers=sort {$aquib} @ some_numbers;, there are few independent sorting subroutines in modern Perl programs.
My@descending=reversesort {$aquib} @ some_numbers; is sorted in descending order of numbers.
My@descending=sort {$bachela} @ some_nubmers; is sorted in descending order of numbers.
6. When you use sort in hashes, you only sort the keys of hash (in alphabetical order). Now we will sort the keys of the hash, which is determined by the value in its corresponding hash.
Subby_socre {$score {$b} $score {$a}}
My%score= ("barney" = >, "fred" = > 205, "dino" = > 30)
My@winners=sortby_scorekeys%score
Subby_score_and_name {
$score {$b} $score {$a} # grades in descending order
Or
Name of $acmp$b;# in alphabetical order
} there can be multiple levels of sorting subroutines in Perl.
Thank you for reading this article carefully. I hope the article "how to use Perl strings and sorting" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.