学习笔记-Perl-记录最简单的一些实例-3
#Example21 #对文件的处理
#!/usr/bin/perl -w
#Usage:perl test1.pl test?.txt #用"?"不用"*"可以加快程序运行
use strict;
chomp(my $date = `date`); #引用系统命令date,取当前系统日期,保存到"$date"中
$^I = ".bak"; #程序自动生成备份文件,并将".bak"追加到名字后面去
while (<>){ #读入txt文件
s/^print/printf/;
s/\#.*\n//;
print;
}
#perl -p -i.bak -w -e 's/print/printf/g' test*.pl.bak
#这是一条命令
#-p自动生成一小段程序 -i.bak 制定后缀名(如果不需要备份,只写-i即可) -w打开警告信息 -e后面跟着的是程序代码
#将test*.pl.bak文件中所有的print改成printf,并生成源文件的备份,将.bak追加到备份文件后面
#于是,我们得到的文件时test*.pl.bak和test*.pl.bak.bak
#-----------------------------------------------------------------------------------------------------------------------------------------------------
#Example21 #Perl程序控制
#!/usr/bin/perl -w
if ($freed =~ /^[A-Z_]\w*$/i){
print "The value of \$freed really look like a Perl identifier name.\n";
} else {
print "The value of \$freed doesn't look like a Perl identifier name.\n";
}
#以上程序与下面的功能完全一样
unless($freed =~ /^[A-Z_]\w*$/i){
print "The value of \$freed doesn't look like a Perl identifier name.\n";
} else {
print "The value of \$freed really look like a Perl identifier name.\n";
}
while($j < $i){
$j*=2;
}
#以上程序与下面的功能完全一样
until($j > $i){
$j*=2;
}
print "The value of \$freed really look like a Perl identifier name.\n" if $freed =~ /^[A-Z_]\w*$/i ;
#以上程序与下面的功能完全一样
if ($freed =~ /^[A-Z_]\w*$/i){
print "The value of \$freed really look like a Perl identifier name.\n";
}
#开开眼界吧,这都什么东西啊,但是他们确实可以执行
&error("Invalid input") unless &valid($input);
$i *= 2 until $i > $j;
print " ", ($n +=2) while $n < 10;
&greet($_) foreach @person;
#-----------------------------------------------------------------------------------------------------------------------------------------------------
#Example22 #定义多个变量
#!/usr/bin/perl
$_ = "Hello there, neigthor!"; #一下子定义三个变量
my($f, $s, $t) = /(\S+) (\S+), (\S+)/;
printf "First: $f\n";
printf "Second: $s\n";
printf "Third: $t\n";
$x = join "\:","$f","$s","$t"; #再连接起来
print "$x\n";
#-----------------------------------------------------------------------------------------------------------------------------------------------------
#Example23 #读入文件,并做统计
#!/usr/bin/perl
while (<>) { #转世操作符,分行读入
foreach (split) { #用空格拆分
$total++;
next if /\W/; #如果该字符串含有特殊字符,跳过下面的
$valid++; #不含特殊字符的正常的单词
$count{$_}++; #统计每个单词个数
}
}
print "Total things = $total, Valid words = $valid!\n"; #输出统计信息
foreach $word (sort keys %count) { #这个很好玩,再猜几次吧
print "$word was seen $count{$word} times.\n";
}
#-----------------------------------------------------------------------------------------------------------------------------------------------------
#Example24 #统计字符串是否曾经出现过
#!/usr/bin/perl -w
my @people = qw{ Fred abc Fred bam bam nm bc nu ;lll};
my %seen;
for (@people) {
print "I've seen you somewhere before,$_!\n"
if $seen{$_}++;
}
#-----------------------------------------------------------------------------------------------------------------------------------------------------
#Example25 #获取某类文件,并存入@ARGV中
#!/usr/bin/perl -w
#第一段的执行方式是: perl test5.pl *.pl
#第二段的执行方式是: perl test5.pl
#foreach $arg (@ARGV) {
# print "One arg is $arg\n";
#}
#my @ARGV = glob "*.pl";
#my @ARGV = <*>;
my $dir = "/bocnet/app_bak/yu/yu_perl";
my @ARGV = <$dir/* $dir/.*>;
foreach $arg (@ARGV) {
print "One arg is $arg\n";
}
#-----------------------------------------------------------------------------------------------------------------------------------------------------
#Example26 #目录句柄
#!/usr/bin/perl -w
my $dir = "/bocnet/app_bak/yu/yu_perl";
opendir DH,$dir or die "Cannot open $dir: $!";
foreach $file (readdir DH) {
print "One file in $dir if $file\n";
}
closedir DH;
- (学习笔记)简单正则表达式的使用实例[javascript]
- Unity学习笔记 一些小疑问解答记录
- (Java2D 学习笔记系列) (一)一个简单的图像填充实例及其分析理解
- mysql学习笔记2--MySQL 触发器简单实例
- 【知了堂学习笔记】Java中static关键字的一些简单使用方法
- 【python爬虫学习笔记】03 requests库爬取的简单实例以及robots协议
- 学习笔记:JAVA RMI远程方法调用简单实例
- Object-C: 学习实例1 一些简单的例子
- Android自动化学习笔记之MonkeyRunner:官方介绍和简单实例
- Eclipse IDE 使用技巧 (Eclipse工作空间小技巧)(自己的一些学习笔记和心得,记录下来,备忘录和复习使用)
- 学习MySQL的一些记录笔记(1)10-28
- Python学习笔记之开发简单爬虫和实例
- tensorflow学习笔记四:mnist实例--用简单的神经网络来训练和测试(MNIST For ML Beginners)
- perl简单学习笔记(一)
- cocos2d-x学习笔记(17)--简单游戏实例1--双枪手
- 学习笔记:JAVA RMI远程方法调用简单实例
- node.js在windows下的学习笔记(2)---简单熟悉一些命令
- 学习笔记:JAVA RMI远程方法调用简单实例
- 【AngularJS学习笔记】封装一些简单的控件(封装成Html标签)