Perl merged text for a piece of instance code _perl

Source: Internet
Author: User

There is such a text file, the content has many lines as follows, the quantity is indefinite.
Lif (__AMSCRIPT_CD ("Www.jb51.net")) {__AMSCRIPT_WC (' #closead {display:none;} ');
Lif (__AMSCRIPT_CD ("Www.jb51.net")) {__AMSCRIPT_WC (' #footer_win {display:none;} ');
Lif (__AMSCRIPT_CD ("Www.jb51.net")) {__AMSCRIPT_WC ('. Mainad {display:none;} ');
Lif (__AMSCRIPT_CD ("Www.jb51.net")) {__AMSCRIPT_WC ('. mt5.recommend {display:none;} ');
Lif (__AMSCRIPT_CD ("Jbxue.net")) {__AMSCRIPT_WC ('. Ggad {display:none;} ');
Lif (__AMSCRIPT_CD ("Jbxue.net")) {__AMSCRIPT_WC ('. ggsidebox {display:none;} ');
............
Requirements merged into:
Lif (__AMSCRIPT_CD ("Www.jb51.net")) {__AMSCRIPT_WC (' #closead, #footer_win,. Mainad,. mt5.recommend {display:none;} ');
Lif (__AMSCRIPT_CD ("Jbxue.net")) {__AMSCRIPT_WC ('. Ggad,. ggsidebox {display:none;} ');

Thought: The URL can be treated as a key, and the merged string is treated as value, so it is stored and printed. It's just a bit of a hassle to print, because the string contains single quotes, double quotes, parentheses, and curly braces, with q# #做为字符串界定符即可.

Copy Code code as follows:

#!/usr/bin/perl
Use strict;
Use warnings;
Sub Test {
My%comments_of_url = ();
Open FILE, "<d:/codesnippets/perl/abc.txt" or Die $!;
while (<FILE>) {
# Skip Empty lines
Next if/^\s*$/;
# Use URLs as key and #xxx as value for each line
# Merge all #xxx for a URL
if (/amscript_cd\ (. *?) " \) \ {__amscript_wc\ (. *?) \s+\{/) {
$comments _of_url{$. = ($. ',' );
}
}
foreach My $key (keys%comments_of_url) {
Chomp (My $value = $comments _of_url{$key});
Print Q{lif (__AMSCRIPT_CD ("};
Print $key;
Print q# ")) {__AMSCRIPT_WC (' #;
Print $value;
Print Q#{display:none;} '); #;
print "\ n";
}
}
Sub Main {
&test ();
}
&main ();

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.