Some Perl Tips

Source: Internet
Author: User

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

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.