How to use split in perl
Editor to share with you how to use split in perl. I hope you will get something after reading this article. Let's discuss it together.
Split syntax perl-- when the split function uses spaces as delimiters, the # # syntax split / PATTERN/,EXPR split / PATTERN/ split split is used to separate the string EXPR, returns the delimited list in the list context, and returns the list length in the scalar context.
Split syntax
# # Syntax
Split / PATTERN/,EXPR split / PATTERN/ split
Split is used to separate the string EXPR, returning the delimited list in the list context and the list length in the scalar context.
Split separates strings with spaces:
#! / usr/bin/perl$_= "a b c d e"; # # string is preceded by a leading space, and the character'a' is preceded by a white space "|", split; # # default delimiter print "\ n"; print join "!", split /\ smatching print "\ n"; # # regular matching space print "\ n"; print join "|", split''; # # direct quantity space print "\ n" # output result # # a | b | c | d | eBay #! A! B! C! D! EBay # a | b | c | d | e
Both the default delimiter split and the direct quantity delimiter split''remove the leading space of the string, while the regular match split /\ sspaces / reserves the leading space and replaces it with a blank character''.
In the past, it was thought that split / / was the same as split', but now it seems to be different.
Why is this happening?!
Replace spaces with -:
#! / usr/bin/perl$_= "--a--b----c-d-e"; print join "!", split /-+ /; print "\ n"; # output result #! A! B! C! D! E
It can be understood that the perl string begins with an empty character'', so you can understand the result separated by split /\ skeeper / in the previous section.
Split''is a special case of split, which simulates the default behavior of awk, so before the splitting behavior starts, all leading spaces in the string are deleted, and then processed with split /\ sarguments /, which is why the two results are different.
In addition to the special case of split'', if other strings are provided, perl will also be interpreted as a regular expression to execute.
After reading this article, I believe you have a certain understanding of "how to use split in perl". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!