本文共 4967 字,大约阅读时间需要 16 分钟。
目录
基本语法
数组
字符串
标量,数组/list,hash的init;引用(数组引用 or hash引用)的init;for 循环的访问(数组or引用)
perl与java 字符串处理比较1 括号
小括号
()是给数组或者hash赋值 整个hash的初始化,用小括号%hash=("foo",35,"bar","12.4"); 列表赋值 ($fred,$barney)=qw (a b c d); ($fred,$barney)=@_; @line=();是置空数组,hash,list 置空都是()中括号
数组下标[] 对匿名列表的引用:将列表的()替换为[]:my $array_r = [1, 2, 3, 4, 5];大括号
对匿名哈希的引用:将哈希的()替换为{}:my $hash_r = { apple => "pomme", pear => "poire" }; 访问hash元素 用大括号{},$hash{"fr"}="flint"; 数组引用展开 my @array = (1, 2, 3, 4, 5); my $array_r = \@array; my @array2 = @{$array_r}; #拷贝了数组2 数组
1) 列表就是数组
2 )用<> 读取了下一行
perl -ne 'print scalar <> if /Subject/' email2.txt 这个等于$a=<>。标量读取,所以一定是一行 为什么加上n不对呢 perl -ne 'print <> if /Subject/' 这个不是读取一行. 这个等于@a=<>。数组读取,所以一定是多行 所以n从wc -l中算出,行不是1,所以继续循环,但是已经读不出东西来了,所以block 3 ) while (<>) { push (@a,"$_"); } #easy print array print @a[-10..-1]; 不能循环即可输出一段数组,用负值输出数组 4 ) perl -pe '@A=<> ; print @A[-10..-1]' 4.txt perl 数组的灵活性。可以范围指定(..)。可以从后向前指定,指定后面的。不用知道长度 #easy print array ,don;t need loop5) @line = ""就是相当于@line = qw("");
@line= 6*7; 只有一个标量的数组6) 数组输出和数组长度输出
$f="@d" ; $f=@d;是标量上下文。数组长度 “@d”这个不是;“”有解析的字符串的意思 “”是转换成字符串 如果是数组, 就会输出数组,然后变成字符串7) #!/opt/exp/bin/perl -w
my @array1 = (10, 20, 30, 40, 50);
my @array2 = ( 1, 2, \@array1, 3, 4); #add "" to add space 10 20 30 40 50 print "@array1";为每个元素加\n,用点符号连接
8) 数组和列表如何转化:split/joint9) 两个数组,和并成一个数组
push (@a,@b);3 string
1) 字符串与数组转化
length后面接标量,不能接数组上下文,所以length数组是1$str="1234567";
@a=split (//,"1234567");
中间什么也不加。可以直接分开没有分隔符的string.列表转成数组
$d1=@d;
$d2=length(@d);
d1=7;d2=1
print $str;
print "str";--------当加上"",perl会自动在每个变量直接加上空格
(1 2 3 4 5 6 7)
$e=@a;-------------表示数组长度
$f="@a";------------数组转成了字符串
$g=join "\n",@d-----------数组转成了字符串
2)string reverse
# 倒置所有行,第一行成为最后一行,依次类推(模拟“tac”)perl -e 'print reverse <>'
# 将行中的字符逆序排列,第一个字成为最后一字,……(模拟“rev”)
perl -ple '$_=scalar reverse'
3): 点号用于连接
# 在每一行后面增加两行空行perl -pe '$_ .= "\n\n"'
x用于重复倍数
# 在每一行开头处插入5个空格(使全文向右移动5个字符的位置)perl -pe 'print " "x5'
4 ^I;regexp;push/pop/正则集合
this function is for add 3 space for each line between { }
1) #!/opt/exp/bin/perl -w
$^I=".bak";
while (<>) {
if (/{/) {
print $_;
push @symbol, '}';
}
if (/}/) {
print $_;
pop @symbol;
}
if (!/{/ && !/}/) {
$number =@symbol;
if ($number>0) {
print " $_";
} else {
print $_;
}
}
2 )
$string =~ s/<.*?>//g;$string =~ s/<.*>//g; 不行perl -pe 's/<.*>//g' ucfirst.pl are also fine
第一个是非最贪心匹配!!!
对于这样的文本例子 abc<abccde>ddef>x 第一个将其转成 abcddef>x 第二个是 abcx or,多选匹配 # 不管是“scarlet”“ruby”还是“puce”,一律换成“redperl -pe 's/scarlet|ruby|puce/red/g' 8.txt
否定匹配
$temp="";while (<>) { if (!/^$/) { print $temp; } $temp=$_;}
区间范围的匹配# 显示两个正则表达式之间的文本(包含)perl -ne 'print if /Iowa/../Montana/'正则掠过空白行和#comment行在指定范围行前加commentsperl -pe 's/^/#/ if ($. >16 && $.<28)' 3-13.plskip 空行和comments行perl -ne 'print $_ unless (/^$/ ||/^#/);' filename# 显示包含65个以下字符的行perl -nle 'print unless /.{65}/'# 删除文件中的所有空行(与“grep ‘.’ ”效果相同)perl -ne 'print if /./'perl -pe 's/\n/ / if $.%2'
正则错误部分
正则替换部分,不能有正则
$string =~ /-?(/d+)/.?(/d+)/;
$string =~ s/-?(\d+)/.$1/;
5 hash/引用
#!/opt/exp/bin/perl -w
#array init
@a=();
@LoL=();
sub function {
$j=@_;
@a=("1","2","3");
print "j=$j\n";
return @a;
}
for $i (1..10) {
@list = function ($i);
#below two is the same ,it is array reference
$LoL[$i] = \@list;
# it is the same with above code
# $LoL[$i] = [ @list ];
# it is array number
$LoL[$i] = scalar @list;
#notes add {} ,reback to array
print "2:@{$LoL[$i]} \n";
#print "2:$LoL[$i] \n";
}
2:
#!/opt/exp/bin/perl -w
sub printSorted {
·#parameter1 is reference for array, parameter2 is hash.@_ list assign
my ($data,%options) = @_;
# get the value for array
print "$data->[3]\n";
#get hash value
print "$options{type}\n";
}
#$list={"1","2","3","4"};
@list=("1","2","3","4");
printSorted (\@list,dir => 'asc',type => 'numerical');
3
#!/opt/exp/bin/perl -w
my @array1 = (10, 20, 30, 40, 50);
my @array2 = ( 1, 2, \@array1, 3, 4);
%hash=(abc=>123, def=>456);
#reference to hash
$ref=\%hash;
#point to hash
print "%$ref\n";
print "$$ref{abc}\n";
4
<span style="font-size:14px;">#!/opt/exp/bin/perl -w
#it doesn't use $_ in hash and array
#array
#set value
@list = (4,3,2,1,0);
foreach $item (@list) {
printf ("%d->$item\n",$i++)
}
#hash
while (my ($key,$value) = each (%ENV)) {
print "$key - $value\n";
}
my @val =sort keys %ENV;
for ($i=0;$i<@val;$i++) {
$key=$val[$i];
print "$key-----$ENV{$key} \n";
}</span>
==
在将hash的keys sort 后,sort是一个独立的循环操作.先这个函数。 map是一个独立的循环,输出key和value, 即按比较顺序输出key-valueprint map {"$_ -> $hash{$_}"} sort {$a cmp $b} keys %hash;
应该是print if aa ; 这种单句处理 上面的sort是一个执行6
1) 读文件,文件转成string
perl 一行即可实现,java需要用while 循环 需要一行一行的读
#file change to string, it is classic , java can;t do it,java just read line one by one
open IN, "<namecfgstr.txt"; #read a text file
my @raw=<IN>; #using @raw store datachomp(@raw); #skip enter keys
2) 正则表达式处理
//it is the same with perl regex, it is good for get match
Pattern patterntrim = Pattern.compile("\\s+(.*),");
经过测试对于一个200k的txt,查找匹配,如果找不到,java 正则需要运行5 分钟。
如果perl处理同上的情况,秒钟解决
转载地址:http://hwfx.baihongyu.com/