Here is a summary of some code snippets in the daily work, some comments are added later, using//This form, do not apply.
1. Get user input
Print ("Please input the date range:");
$dateRange =<stdin>;
Chomp ($dateRange);
2. Exit the program if the data does not meet the requirements
if (!isvaliddaterange ($dateRange)) {
Die ("Wrong date range\n");
}
3. Splitting a string to get an array
My @arrDate =split (/\s*[~,-]\s*/, $dateRange);
[Email protected] [0];
[Email protected] [-1]; The second-to-last, Perl array of this feature is really sweet, more than the length of a C-pulse to save a lot of trouble, but there are elsif this kind of the wayward design of what I do
4. Print text on the screen
Print ("Reading file...\n");
5. Progressive reading of files
My @arrFileFound;
Open (FOF, $fof) | | Die "Could not read $fof: $!";
while ($line = <FOF>) {
# read a line from file
Chomp ($line);
# fill the Arrfilefound with line
Push (@arrFileFound, $line); The read-out rows are put into an array
}
Close (FOF);
6. Array sorting
@arrFileFound =sort (@arrFileFound);
7. Iterating through an array
# Initialize Array
@annoNames = ("Danile", "Iria", "Jinwn", "Ka", "Manzhez", "Marna", "Max_nglish", "mohmed", "Roxna", "Thir", "Trno", "Trno_ 中文版 "," transtc_en ");
[e-mail protected];//to take the array length
$iNames = 0; Traverse subscript
while ($iNames < $nNames) {
$annoName = $annoNames [$iNames];
# do STH
...
$iNames + +;
}
8. Passing an array as a parameter to a function
function Definition:
Sub printannologs{
Local ($annotator, *files) [email protected]_;
[Email protected];
$i = 0;
while ($i < $n) {
My [email protected][$i];
...
$i + +;
}
}
Invocation Mode
&printannologs ($annoName, *annologs);
9. Determine if a string contains another string
Sub isvalidannotator{
Local ($name) [email protected]_;
My $names = "Danile", "Iria", "Jinwn", "Ka", "Manzhez", "Marna", "Max_nglish", "mohmed", "Roxna", "Thir", "Trno", "Trno_ Entac_en ";
return index ($names, $name)!=-1;
}
10. Regular expression pattern matching
Sub isvaliddaterange{
Local ($date) [email protected]_;
return $date =~/^\d{8}\s*[-~,]\s*\d{8}$/;
}
Related article: http://www.cnblogs.com/xiandedanteng/p/3250688.html
Some Perl Tips