#! / usr / bin / perl
my $ tmp = "ye_qing";
my @name = split ("_", $ tmp);
my $ list = "== $ name [0] == $ name [1] == \ n";
print $ list;
my @ ye = (1,2,3,4);
my @ qing = (1,2, @ ye, 4,5,6);
$ scount = @qing;
print "== sum: $ scount == \ n";
print "[email protected] == \ n";
$ count = 1;
while ($ count <[email protected]) {
print ("== age: $ qing [$ count-1] == \ n");
$ count ++;
}
@sortList = sort (@qing);
print "--sort: [email protected] \ n";
########## will remove the last character of the keyboard input string-line break
@list = ("rabbit", "12345", "yeqing");
chop (@list);
print "[email protected] $ list [0]-$ list [1]-$ list [2]-\ n";
########## Concatenate arrays and strings with :::
$ string = join ("::", @ list, "hello world !!!");
print "-$ string-\ n";
######### Split the string into an array according to :::
@ arr = split (/ :: /, $ string);
print "[email protected] ==== \ n";
######### hash
% h = (‘a’ => 1, ’b’ => ‘_’, c => ‘hello’);
print "hash:-$ h {a}-$ h {b}-$ h {c}-\ n";
##### Remove an element from the hash
delete $ h {b};
print "hash:-$ h {a}-$ h {b}-$ h {c}-\ n";
###### Empty hash
undef% h;
print "hash:-$ h {a}-$ h {b}-$ h {c}-\ n";
##### Get all keys in the hash
% h = (‘a’ => 1, ’b’ => ‘_’, c => ‘hello’);
@all_key = keys% h;
print "[email protected] _key == \ n";
##### hash sort
#All key values are arranged in descending order by hash value. The comparison of values is a numerical comparison (for example, 10> 9)
@all_keys = sort {$ h {$ b} <=> $ h {$ a}} (keys% h);
# All key values are arranged in descending order according to the hash value. Comparison of values is numeric comparison
@all_keys = sort {$ h {$ a} <=> $ h {$ b}} (keys% h);
# All key values are arranged in descending order according to the hash value. The comparison of values is a string comparison (for example, ‘10’ <‘9’)
@all_keys = sort {$ h {$ a} cmp $ h {$ b}} (keys% h);
#Determine if the hash contains a key
exists ($ h {$ key});
########## How much data is stored in the output hash
print scalar keys% h, "\ n";
#Traversing a hash
while (my ($ k, $ v) = each% h) {print "$ k ---> $ v \ n";}
#### while loop
# last is the loop that appears in the next loop, next is the next loop that skips the instructions below and executes directly.
while (chomp ($ i = 100100100100100100100100100100)) {
next if ($ i == 5);
last unless ($ i> 10);
}
#################### Example
# Sum a set of numbers and print.
my $ s1 = & sumvar (11,22,33);
my $ s2 = & sumarg (22,33,44);
my $ s3 = & sumgod (11,22,33,44,55);
print "s1 = $ s1, s2 = $ s2, s3 = $ s3 \ n";
# Option 1
sub sumvar {
# Assign the first three elements of the parameter array to ($ first, $ second, $ third) accordingly
(my $ first, my $ second, my $ third) = @_;
# Returns its sum value. Disadvantages: If you are summing four parameters, you can still only give the first three.
return $ first + $ second + $ third;
}
# Option 2
sub sumarg {
# $ _ [0] represents the first element of the parameter array @_. The rest by analogy.
my $ first = $ _ [0];
my $ second = $ _ [1];
my $ third = $ _ [2];
#Returns its sum value. Cons: Same as sumvar. Just learn the usage of $ _ [0] here.
return $ first + $ second + $ third;
}
# Option 3, you can have as many parameters as you like. Can be summed up.
sub sumgod {
my $ s = shift @_;
foreach (@_) {
$ s = $ s + $ _;
}
#Same as the previous function max.
return $ s;
}
#################### to sum up
Data manipulation
* $-Declare and reference a scalar variable
* @-Declare and reference a list, but when accessing members of a list, use $ ListName [index]
*%-Declare and reference a hash table, but when accessing a hash member, you need to use $ HashName
{key}
Special variables
* $ 0-filename of the currently running script
* @ARGV-list of command line arguments for the currently running script
* $ _-Default variable, such as the current variable in the loop
* @_-list of input parameters for the function
*% ENV-system environment variables
* @INC-Perl's list of Include paths, we can add our own directory to this list for easy reference
Use a custom library
* $!-Current system prompt, error message
* $ ^ O-the name of the operating system
* STDIN, STDOUT, STDERR-default handle for input and output, can be customized
* =>-When declaring a hash, it can be used to clearly indicate the corresponding relationship of key => value
* $ ^ I- Specify the suffix of the backup file. In this way, the modified file will automatically save a file with the suffix
this
Special usage
* & Sub-call a function, although Perl has some rules that allow you to omit the ampersand here, but
It is considered for consistency, so I always use this method for custom function calls.
* $ #-Used to get the maximum index of the modulo array, in general, you can also use -1 to represent the last element
Index
* qw ()-Quickly declare an array of strings, you can omit those annoying quotes
Regular expression
* $-Get matches captured by parentheses
* $ `, $ &, $‘-Get the matched string and its two parts before and after
* ^, $-Start and end position of the string for positioning
Common functions
* pop, push, shift, unshift, reverse-list operation functions
* keys, values, exists, each, delete-hash operations
* chomp, split, join, index, substr, sort-string manipulation functions
* sprintf, printf, print-Format output functions
* system, exec, ``-system command call function
* glob, unlink, mkdir, rmdir, rename, chmod, chown, open, close, opendir,
closedir-file system manipulation functions
* stat, lstat, localtime, gmtime, utime-document properties, time related functions
* hex, oct-functions for converting binary, octal, and hexadecimal numbers to decimal
* grep, map-list advanced operation functions
#The detailed introduction of these functions can be done by command:
#perldoc -f functionname
#Find common libraries
* File :: Basename-Get file name or file path based on path
* File :: Spec-Combine entire path based on file name and path
* File :: Find-recursively traverse all files in a directory
* XML :: Simple-a complex structure to represent xml files, which is quite convenient to use
* Time :: HiRes-often used to calculate the time spent on an operation
* Getopt :: Long-used when the script requires complex input parameters and options
* Cwd-get the current working directory
* IO :: File-File operations
This article is from the "Technology Filigree" blog, and is not reprinted!
perl study notes