Study Perl cookbook

Source: Internet
Author: User
Tags unpack

1:Substring Truncation

 

$ Value = substr ($ string, $ offset, $ count );

$ Value = substr ($ string, $ offset );

 

Substr ($ string, $ offset, $ count) = $ newstring;

Substr ($ string, $ offset, $ count, $ newstring );#Equivalent to the previous statement

Substr ($ string, $ offset)= $ Newtail;

In some casesUnpackMore convenient Functions,AlthoughUnpackIs read-only,But it is more efficient.,You can retrieve multiple substrings at a time.

#First5Bytes,Skip3Bytes,Retrieve again2Items8Bytes,Finally, retrieve the remaining

#(Note::Only supportedASCII,Not SupportedUnicode)

($ Leading, $ S1, $ S2, $ trailing) = unpack ("A5 X3 A8 A *", $ data );

 

#Per5Split byte into Arrays

@ Fivers = unpack ("A5" X (length ($ string)/5), $ string );

 

#Split into single-character Arrays

@ Chars= Unpack ("A1" x length ($ string), $ string );

 

 

 

2.Set the Initial Value

 

#Set$ BAssigned$,If$ BNot true,Then$ CAssigned$

$ A = $ B | $ C;

 

#When$ XWhen not true,Set$ YAssigned$ X

$ X | = $ Y;

When 0 ," 0 ", And""Is the valid value of the Variable,Variables can be defined in this way.:

#Set$ BAssigned$,If$ BNot Defined,Then$ CAssigned$

$ A = defined ($ B )? $ B: $ C;

 

#FuturePerlIt supports "new" or defining operators.

$ A = $ B // $ C;

 

 

3.No temporary variable exchange value

 

($ Var1, $ var2) = ($ var2, $ var1 );

 

In fact, we can exchange more than this.2Variable (s). It is very convenient to directly exchange multiple variables.

 

 

4.Conversion of characters and values

 

$ Num= Ord ($ char );

$ Char = CHR ($ num );


To be continue .......

 

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.