Perl common functions (suitable for beginners)!

xiaoxiao2021-03-06  97

Directive: Print Syntax: Print FileHandle List Description: This FileHandle can be seen as a bridge between I (Input) / O (Output), you can use FileHandle to make data read, write .STDIN is from which Enter data, such as input from the computer; STDOUT is representing which connection output data; for example, from the computer's screen output; STDERR is data from which to output errors, such as from the computer's screen output. And in Perl language. There are three standard filehandle: 1.stdin: is a filehandle2.stdout representing stdin: is a FileHandle3.stderr representing stdout: It is the fileHandle representing stderr if you want to use other filehandle , Use the Open this function to open a filehandle, we can use the print of this function list to FileHandle. Before introducing this function to this function, let's take a look at the special print character in the print function: instruction: # Description: Note Symbol Remark Declaration example: # This is an annotation illustration ---------------------------------------------------------------------------------------------------------------------- -------------------------------------------- Directive: Print Syntax: Print FileHandle List Description: This FileHandle can be seen as a bridge between I (Input) / O (Output), can use FileHandle to make data read, write .Stdin is the representative to enter data, for example from The computer keyboard input; STDOUT is representing which connection output data; for example, from the computer's screen output; STDERR is data representative, such as from the computer's screen output. There are three standard FileHandle: 1 in the Perl language. .Stdin: It is fileHandle2.stdout representing stdin: It is a filehandle3.stderr representing stdout: It is Open to use Open when you want to use other FileHandle if you want to use other FileHandle if you want to use other filehandle Function to open a FileHandle, we can use the Print this function list to FileHandle. Let's take a look at the Print function before introducing this function. Special printing characters: symbols its effects / N wrap new line / R cursor wrap RETURN / T tab key / F change form feed / b return one / V vertical Tab key / a ring Bell / E ESCAPE / 007 decimal ASC II code / XFF hexading code / C [Control character example: print "online school / N"; Plus "Online Academy" plus the wrap on the screen. Syntax: Print List Description: If filehandle is omitted The fileHandle will be fixed to stdout. That is, the data content of the list is displayed on the screen. Example: $ URL = "www.netease.net/~zmd"; Print "Online Academy $ URL / N"; "Online Academy www.netease.net/~zmd" will appear on the screen, if you want to make the variables in the double quotes invalid, you can add "/" symbol in front of the variable. For example: Print "Online Academy / $ URL "; this is displayed:" Online Academy $ URL "Syntax: Print Description:

If the filehandle and list will be omitted, it will be filehandle, and the data content of $ _ this memory output variable will be output. If the $ _ variable is an empty string, an empty string is displayed. Example: $ _ = "Online Academy / N"; Print; will add "online school" plus the wrap on the screen --------------------- -------------------------------------------------- ------- Directive: PrintF Syntax: Printf FileHandle List Description: In the Perl language, it is also the same as the syntax, usage and C language in the C language. If FileHandle is omitted, it will STDOUT is a constant FileHandle. Let's take a look at the characters of the transformed symbol in the printf function before introducing the Printf function. Symbols It effects% C character% S Stroke% D integer% F floating number% h 16 Entering code% O 8 feed code example: Printf ("chomod% D% S / N", "711" "CGI"); the CHMOD 711 CGI will be displayed on the screen. -------------------------------------------------- ------------------------------ Command: Chop syntax: Chop ($ URL) Description: Delete the last character. Example: $ URL = "www.nease.net/~zmd /"; Chop ($ URL); At this time, $ URL = "www.nease.net/~zmd" and these two lines can also be written ($ URL = "www.nease.net/~zmd/"); ------------------------------------- ------------------------------------------- Directive: Split Syntax: Split / pattern /, $ text, limit) where / pattern / is a pattern of text processing, and Limit is a number of numbers to be split, generally can be omitted. Description: Use a specified text processing mode to split $ TEXT strings. Example: $ text = "michael, gevin, mike"; @ name = split (/, /, $ text); # @Name = ("Michael", "Gevin", "Mike"); ($ A, $ b, $ c) = split (/, /, $ text); # This time $ a = "michael"; $ b = "gevin"; $ c = "mike"; @ name = split (/, $ String, 2); # This time @Name = ("Michael", "GEVIN"); encoding the data first, where the data content in the FORM is used & This symbol is spaced apart, so in decoding, it is necessary to divide each data field in the decoding character.

For example: $ text = "Mike = a & michael = b"; @ name = split (/ & /, $ text); # @Name = ("Mike = a", "michael = b); and data field The value of the name and this data field is separated from = this symbol. If you want to get the name of the data field and the corresponding value, use the = this symbol to split the data field, for example: $ name = "" Mike = Michael ""; ($ Name1, $ Name2) = split (/ = /, $ list); # $ name1 = "mike"; $ name2 = "michael"; ----------- -------------------------------------------------- ------------------- Command: Keys Syntax: Keys (% array) Description: Remove all KEY in the associated array% Array. Example:% Name = (1, "Mike", 2, "Michael"); @ readkey = keys (% names); # @ Readkey = (1, 2); ---------- -------------------------------------------------- -------------------- Command: VALUES Syntax: VALUES (% array) Description: Remove all Value in the associated array% Array. Example:% name = (1, "mike", 2, "michael"); @ readval = values ​​(% names); # @readval = ("Mike", "michael"); ------ -------------------------------------------------- ------------------------ Directive: Reverse Syntax: Reverse (@Array) Description: Re-arrange the elements in the array @Array. Example: @Back = ("a", "b", "c", "d", "e"); @ back = Reverse (@Back); # @Back = ("e", "d" , "C", "B", "A"); ------------------------------------ -------------------------------------------- instructions: sort syntax: sort (@Array) Description: Put the elements in the array from a small to large, if you want to be sorted by large to small, add the REVERSE this function.

Example: @ABC = ("D", "B", "C", "A"); @ ABC = Sort (@ABC); # @abc = ("a", "b", "c" , "D"); @ ABC = (Reverse Sort @ ABC); # @ABC = ("D", "C", "B", "A"); this syntax can also be written @ ABC = (Reverse sort (@abc)); @ number = (5, 2, 10); @ number = sort (@Number); When the above example is sorted by the sort function, the error is wrong, so it is necessary to use the following to the following sentence. . @ Number = (sort {$ a <=> $ b} @number); # @ Number = (2, 5, 10); ------------------ -------------------------------------------------- ------------ Directive: Length Syntax: Length Description: Stroke a string $ string byte (Bytes) value. Example: $ string = "perl5"; $ size = length ($ string); # $ size = 5; ------------------------ -------------------------------------------------- ------ Command: Substr Syntax: Substr ($ String, Offset, Length) Offset represents the position of the start character, the length of the referenced string, if you omit the last value to the string from the start value to the string A character length. If Offset If is a negative value, the specified character will start from the right side of the string. Example: $ s = SUBSTR ("Perl5", 2, 2); # This time $ s = "rl"; $ s = substr ("Perl5", 2); # = $ s = "rl5"; $ s = SUBSTR ("Perl5", - 2, 2); # $ S = "ER"; --------------------------- -------------------------------------------------- --- Directive: Index Syntax: INDEX ($ String, $ Substring, Position) $ SUBSTRING is the character to be looking for; the position represents which position begins to find, if the Position is hosted from the beginning. Note: Returns the characters you want to find in a string $ String, if you can't find characters in the string, the value is returned.

Example: $ s = index ("Perl5", "P"); # = 0 $ s = index ("Perl5", "L", 2); # = $ s = 3 $ s = index ("Perl5", "Perl"); # = = -1 -------------------------------- ------------------------------------------------instruction: Push Syntax: Push (@ Array, $ String) Description: At the last additional new element ($ String) to the array @Array in array @Array. Example: @Array = ("One", "TWO"); Push (@Array, "Three"); # @ @ array = ("one", "two", "three") ----- -------------------------------------------------- ------------------------- Directive: POP syntax: POP (@Array) Description: Delete the last element of the array (@Array), and Return the deleted element. Example: @Array = ("One", "Two"); $ RM = POP (@Array); # @Array = ("one"); and $ rm = "two"; ------ -------------------------------------------------- ------------------------ Command: UNSHIFT Syntax: UNSHIFT (@ Array, $ String) Description: Additional in front of array @Array New elements $ string to array @Array. Example: @Array = ("One", "Two"); unshift (@Array, "three"); # @Array = ("Three", "One", "Two") ------ -------------------------------------------------- ------------------------ instruction: Shift Syntax: Shift (@Array) Description: Delete the first element of the array @Array and will be deleted Elements returned. Example: @Array = ("One", "Two"); @ rm = shift (@Array); # @Array = ("two"); and $ rm = "one"; ------ -------------------------------------------------- ------------------------ Directive: Join Syntax: Join ($ String, @ array) Description: Plus between the elements of a group @Array A specified character $ string and returns the result.

Example: @Array = ("One", "Two", "Three"); $ TOTAL = JOIN (":", @ array); Total = "ONE: Two: Three"; ----- -------------------------------------------------- ------------------------- Command: GREP syntax: grep (/ pattern /, @ array) Description: Transferring the text processing mode (Regular Expression) Array elements come out. Example: @Array = ("One", "on"); $ count = grep (/ on /, @ array); # $ count = 2 @ result = grep (/ on /, @ array ); # @Result = ("one", "on"); -------------------------------- ------------------------------------------------instruction: HEX Syntax: HEX ($ String) Description: Turn a hexadecimal value into a decimal. Example: $ decimal = HEX ("ff"); at this time $ decimal = 255; ------------------------------- -------------------------------------------------instruction : RAND Syntax: Rand ($ Interger) Description: The normal and function SRAND matching a random number, if the Stand function is not declared first, the retrieved constant value is a fixed value. This grammar returns a value between 0 and $ INTERGER. If the $ INTERGER is omitted, a value of 0 and 1 will be returned. Example: SRAND; # To declare the SRAND function can produce the effect of the random number $ INT = rand (10); # $ int for greater than 0 and less than 10 If you want to generate the mess, you must add INT # This function $ INT = Int (Rand (10)); # $ int value is an integer, and the value is between 0 and 9 ---------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------- ------------ Command: LocalTime Syntax: LocalTime (TIME) Description: You can return nine-related time elements, which often use the system time when writing CGI applications, so here Detail usage of this function. Example: ($ Sec, $ MIN, $ YEAR, $ WDAY, $ YDAY, $ ISDST) = localtime (time); where: $ sec represents the number of seconds [0,59] $ min Representative score [0,59] $ HOUR 小 小 [0,23] $ MDAY representative is the first few days in this month [1,31] $ mon represents the month [0,11], to add $ mon plus 1 After that, you can meet the actual situation. $ Year From 1990, $ WDAY is on Saturday, the representative is in the first few days in this week [0-6] $ yday counting from January day, the representative is the first year in this year. Day [0,365] $ ISDST is just a FLAG knows that these variables can be applied in the CGI application.

转载请注明原文地址:https://www.9cbs.com/read-104554.html

New Post(0)