用perl打开一个word文件并修改部分表格和文本内容,
基于ASCII码排序 复制代码 代码如下:#!/usr/bin/perl use strict; use warnings; use 5.010; use Data::Dumper qw(Dumper); my @words = qw(foo bar zorg moo); say Dumper \@words; my @sorted_words = sort @words; say Dumper \@sorted_words; 上边的例子将会打印 复制代码 代码如下:$VAR1 = ['foo','bar','zorg','moo' ];$VAR1 = ['bar','foo','moo','zorg' ]; 第一个输出显示了排序前的数组,第二个是排序后的。
这是最简单的情形,但是可能未必是你想要的。
比如,如果一些单词以大写字母开头怎么办?复制代码 代码如下:my @words = qw(foo bar Zorg moo); @sorted_names里的结果将是:复制代码 代码如下:$VAR1 = ['Zorg','bar','foo','moo' ]; 你会发现,以大写字母开头的单词排在了第一位。
这是因为sort默认根据ASCII码表排序,所有的大写字母都排在小写字母前边。
比较函数 Perl的sort的工作方式是这样的,它遍历原始数组的每两个元素;每次把左边的值放入变量$a,把右边的值放入变量$b。
然后调用比较函数。
如果$a的内容应该在左边的话,“比较函数”会返回1;如果$b应该在左边的话,返回-1,两者一样的话,返回0。
通常你看不到比较函数,sort会根据ASCII码表对值进行比较,不过如果你想的话,你可以显式的写出来:复制代码 代码如下:sort { $a cmp $b } @words; 这段代码会跟没有使用块的sort @words达到同样的效果。
这里你可以看到,默认perl使用cmp作为比较函数。
这是因为正是cmp可以做这里边我们需要的工作。
它比较两边的字符串的值,如果左边参数“小于”右边参数,就返回1;如果左边参数“大于”右边参数,就返回-1;如果相等,就返回0。
按字母顺序排列 如果你想忽略字符串的大小写来排序——即通常所谓的字母序,你可以像下一个例子这么做:复制代码 代码如下:my @sorted_words = sort { lc($a) cmp lc($b) } @words; 这里为了比较,我们调用lc函数返回参数的小写版本。
然后cmp比较这些小写版本并决定原始字符串谁先谁后。
结果是 复制代码 代码如下:$VAR1 = ['bar','foo','moo','Zorg' ]; Perl对数值排序 如果对数值数组使用sort进行默认的排序,结果可能不是我们期望的。
复制代码 代码如下:my @numbers = (14, 3, 12, 2, 23); my @sorted_numbers = sort @numbers; say Dumper \@sorted_numbers;$VAR1 = [12,14,2,23,3 ]; 仔细一想的话,这并不奇怪。
比较函数看到12和3时,它按字符串进行比较。
这意味着比较两个字符串的第一个字符"1"和"3"。
在ASCII码表里,"1"在"3"前边,因此字符串"12"会排在字符串"3"前面。
Perl不会很神奇地猜到你想按数字对这些值排序。
尽管我们可以写一个比较函数来按数字比较两个值。
但这里我们使用(也被称作宇宙飞船操作符), 它会按数字来比较两个参数并返回1、-1或者0。
复制代码 代码如下:my @sorted_numbers = sort { $a $b } @numbers; 结果是:复制代码 代码如下:$VAR1 = [2,3,12,14,23 ];
如何用perl来遍历一个文件夹下的所有文件夹,找出所有名字中带xx的...
展开全部 use strict;use Win32::Word::Writer;my $oWriter = Win32::Word::Writer->new();#Adding text and paragraphs with different styles$oWriter->WriteParagraph("Example document", heading => 1); #Heading level 1$oWriter->WriteParagraph("Usage", style => "Heading 2"); #Style "Heading 2"$oWriter->WriteParagraph("Write sentences to the document using a"); #Normal$oWriter->WriteParagraph("heading level, or Normalif none is specified. "); #\n is new paragraph$oWriter->Write("Add some more text the current paragraph");$oWriter->NewParagraph(style => "Envelope Return"); #The style must exist$oWriter->Write("Return to sender. ");$oWriter->SetStyle("Envelope Address"); #Change the current style$oWriter->Write("Nope, we changed the style of the entire paragraph");$oWriter->Write("to a footer style");#Setting character styles$oWriter->WriteParagraph("Some more normal text. ");$oWriter->SetStyle("Hyperlink"); #A charachter style$oWriter->Write("http://www.DarSerMan.com/Perl/");$oWriter->ClearCharacterFormatting(); #Clear character style$oWriter->Write(" ToggleBold(); #Toggle bold$oWriter->Write("Perl ");$oWriter->SetItalic(1); #Turn on Italic$oWriter->Write("stuff.");$oWriter->ToggleItalic(); #Toggle Italic$oWriter->SetBold(0); #Turn off bold#Bullet point lists$oWriter->ListBegin();$oWriter->ListItem();$oWriter->Write("The first bullet item");$oWriter->ListItem();$oWriter->Write("The second bullet item");$oWriter->ListBegin(); #Nested bullet point list$oWriter->ListItem();$oWriter->Write("The first inner bullet item");$oWriter->ListItem();$oWriter->Write("The second inner bullet item");$oWriter->ListEnd();$oWriter->ListEnd();#Do this at regular intervals (say, every couple of 10K of text you add)$oWriter->Checkpoint();#Tables$oWriter->WriteParagraph("Table example", heading => 1);$oWriter->NewParagraph();$oWriter->TableBegin();$oWriter->TableRowBegin();$oWriter->TableColumnBegin();$oWriter->SetBold(1);$oWriter->Write("HTML table");$oWriter->TableColumnBegin();$oWriter->Write("Win32::Word::Writer");$oWriter->TableRowBegin();$oWriter->TableColumnBegin();$oWriter->SetBold(0);$oWriter->Write("");$oWriter->TableColumnBegin();$oWriter->Write("TableBegin()");$oWriter->TableRowBegin();$oWriter->TableColumnBegin();$oWriter->Write("");$oWriter->TableColumnBegin();$oWriter->Write("TableRowBegin()");$oWriter->TableEnd();#Save the document$oWriter->SaveAs("01example.doc");
关于perl的index用法
具体用法如下:Theindex()function is used to determine the position of a letter or a substring in a string. For example, in the word "frog" the letter "f" is in position 0, the "r" in position 1, the "o" in 2 and the "g" in 3. The substring "ro" is in position 1. Depending on what you are trying to achieve, theindex()function may be faster or more easy to understand than using a regular expression or thesplit()function.Let's say you are trying to determine if a word has a particular letter in it. Let's look for the letter "l" in the string "perlmeme.org".#!/usr/bin/perluse strict;use warnings;my $string = 'perlmeme.org';my $char = 'l';my $result = index($string, $char);print "Result: $result\n";This program gives you:Result: 3If the string doesn't contain the letter,index()will return a -1. For example, we can look for the letter "L" in the string "perlmeme.org":#!/usr/bin/perluse strict;use warnings;my $string = 'perlmeme.org';my $char = 'L';my $result = index($string, $char);print "Result: $result\n";The program outputs:Result: -1If the letter we're searching for appears more than once in the string,index()return the index of the first occurrence of the letter.#!/usr/bin/perluse strict;use warnings;my $string = 'perlmeme.org';my $char = 'e';my $result = index($string, $char);print "Result: $result\n";This program gives you:Result: 1Looking for a substring (rather than a single character) works in exactly the same way as looking for a single character:#!/usr/bin/perluse strict;use warnings;my $string = 'perlmeme.org';my $substr = 'me';my $result = index($string, $substr);print "Result: $result\n";This program gives you:Result: 4Theindex()function let's you specify an offset. This tellsindex()where to start looking in the string. In our example, we found the first occurrence of the letter 'e' at position 1. Let's try to find the second:#!/usr/bin/perluse strict;use warnings;my $string = 'perlmeme.org';my $char = 'e';# The first 'e' was at position 1, so let's start# looking again at position 2my $offset = 2;my $result = index($string, $char, $offset);print "Result: $result\n";The program outputs:Result: 5To find (and do something with) every occurrence of a character in a string, you could useindex()in a loop, incrementing the offset each time:#!/usr/bin/perluse strict;use warnings;my $string = 'perlmeme.org';my $char = 'e';my $offset = 0;my $result = index($string, $char, $offset);while ($result != -1) {print "Found $char at $result\n";$offset = $result + 1;$result = index($string, $char, $offset);When we run this program, we get the following output:Found e at 1Found e at 5Found e at 7Instead of looping through every occurrence of a letter in a string to find the last one, you can use therindex()function. This works exactly the same asindex()but it starts at the end of the string. The index value returned is string from the start of the string though.#!/usr/bin/perluse strict;use warnings;my $string = 'perlmeme.org';my $char = 'e';my $result = rindex($string, $char);print "Result: $result\n";This would produce:Result: 7
有没有办法使word下录制的任一宏遍历指定目录下的若干doc文档?
想到一个办法:将下面两行加在所需运行宏"所需运行宏"的末行之前:ActiveDocument.SaveActiveWindow.Close另建一宏"YYYY遍历":Sub YYYY遍历()Dim myDialog As FileDialog, oFile As Variant, oDoc As DocumentSet myDialog = Application.FileDialog(msoFileDialogFilePicker)With myDialog.Filters.Clear.Filters.Add "所有 WORD 文件", "*.doc", 1.AllowMultiSelect = TrueIf .Show -1 Then Exit SubFor Each oFile In .SelectedItemsSet oDoc = Documents.Open(FileName:=oFile, Visible:=True)With oDoc''''''''''''''''''''''''''''''''''''''''''''''''''With oDoc.RangeApplication.Run MacroName:="所需运行宏"End With''''''''''''''''''''''''''''''''''''''''''''''''''End WithNext oFileEnd WithEnd Sub在word中运行宏"YYYY遍历"即可。
不好意思,自己回答了。
求解释这个perl脚本,非常感谢
warning:Use of uninitialized value in pattern match (m//) at processMonitor.pl line 38上面这个warn是因为while循环条件里面少了东西,这里应该是这个句柄另外,那个exec要执行logger,这个程序有的不在/bin目录下,而在/usr/bin目录下这个程序大致意思是查看指定的程序是在ps列表中出现的次数例如,你在命令行执行这个程序./processMonitor.pl ps klogd就可以在生成的log文件中看到结果了下面给你稍微加了些注释,如果基本语法比较熟悉的话,应该可以看懂了#!/usr/bin/perl -w##name: processMonitor.pl#written by trizsolo#usage: ./processMonitor.pl args args args#you can add as many processes as needed for args#or use the defaults... syslogd is always checked#----------------------------my $user='root';my $host=`hostname`;chomp($host);my $date=`date`;chomp($date);my $logfile="$host" . '.log';#以主机名加log后缀的形式命名日志文件our @proc_list=@ARGV;# Default list of processesif(@proc_list==0)#如果不在命令行指定程序名,则默认查看下面的三个程序{# check for arguments @proc_list=('inetd', 'sendmail', 'chkMounts.pl');}push(@proc_list, 'syslogd');# add syslogd to check# ---------- open logfile and determine Operating System --------------#以添加的方式打开日志文件,并把要查看的程序名称,日期及系统信息添加到日志文件open(LOG, ">>$logfile") or die "Can't open $logfile to write: $!";print LOG "Searching for: @proc_list on $date!\n";chomp($os=`uname -r`);print LOG "OS on this server is $os\n";close(LOG);# ---------------- call "ps" & analyse output --------------------#初始化哈希计数表,用来统计指定程序在列表中出现的次数foreach my $process (@proc_list){ $event_count {$process} = 0;}#使用管道,使ps的输出作为下面匹配的目标open(PS, "/bin/ps auwx |") or die "Can't run ps: __FILE__ $!";$/="\n";# record seperator#这个while原来少了东西,应该有上面打开的句柄while(){ foreach my $process(@proc_list) { if($_ =~(m/$process/i)) { $event_count {$process}++; } }}close(PS);再次打开日志文件,把上面遍历ps列表后的信息写入日志文件open(LOG,">>$logfile") or die "Cannot open $logfile: $!";# Add line to log to nagios if neededforeach $process(@proc_list){ if($event_count {$process} ==0) { print LOG "Process $process is NOT running!\n";#这里的logger有的在/usr/bin目录下 system("/bin/logger -p warn Process: $process is NOT running!\n") == 0 or die "Cannot complete cmd: $! : $?"; } else { print LOG "Process $process occurred $event_count{$process} times!\n"; }}close(LOG);exit 0;#EOF
perl脚本统计一列中重复
while($line=){chomp $line;if (not exist $results{$line}){$results{$line} = 1;}else{$results{$line}++;}}foreach (keys %results){if ($results{$_} == 1){print $_."不重复\n";}else{print $_."重复".$results{$_}."次\n";}}
perl 匹配重复字符串
Perl split函数Perl中的一个非常有用的函数是Perl split函数-把字符串进行分割并把分割后的结果放入数组中。
这个Perl split函数使用规则表达式(RE),如果未特定则工作在$_变量上。
Perl split函数可以这样使用: $info="Caine:Michael:Actor:14,LeafyDrive"; @personal=split(/:/,$info); 其结果是:@personal=("Caine","Michael","Actor","14,LeafyDrive");◆如果我们已经把信息存放在$_变量中,那么可以这样:@personal=split(/:/);如果各个域被任何数量的冒号分隔,可以用RE代码进行分割: $_="Capes:Geoff::Shotputter:::BigAvenue"; @personal=split(/:+/); 其结果是:@personal=("Capes","Geoff","Shotputter","BigAvenue");但是下面的代码:$_="Capes:Geoff::Shotputter:::BigAvenue"; @personal=split(/:/); 的结果是:@personal=("Capes","Geoff","","Shotputter","","","BigAvenue");◆这个Perl split函数中单词可以被分割成字符,句子可以被分割成单词,段落可以被分割成句子:@chars=split(//,$word); @words=split(//,$sentence); @sentences=split(/\./,$paragraph); 在第一句中,空字符串在每个字符间匹配,所以@chars数组是一个字符的数组。
>>//之间的部分表示split用到的正则表达式(或者说分隔法则)\s是一种通配符,代表空格+代表重复一次或者一次以上。
所以,\s+代表一个或者一个以上的空格。
split(/\s+/,$line)表示把字符串$line,按空格为界分开。
转载请注明出处51数据库 » perl遍历word
女3神经