博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
perl 总结
阅读量:277 次
发布时间:2019-03-01

本文共 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 loop

5) @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/joint

9) 两个数组,和并成一个数组

push (@a,@b);

3  string

1) 字符串与数组转化

length后面接标量,不能接数组上下文,所以length数组是1

  1. $str="1234567";

  2. @a=split (//,"1234567");

中间什么也不加。可以直接分开没有分隔符的string.列表转成数组

  1. $d1=@d;

  2. $d2=length(@d);

  3. d1=7;d2=1

  4. print $str;

  5. print "str";--------当加上"",perl会自动在每个变量直接加上空格

  6. (1 2 3 4 5 6 7)

  7. $e=@a;-------------表示数组长度

  8. $f="@a";------------数组转成了字符串

  9. $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

  1. $^I=".bak";

  2. while (<>) {

  3. if (/{/) {

  4. print $_;

  5. push @symbol, '}';

  6. }

  7. if (/}/) {

  8. print $_;

  9. pop @symbol;

  10. }

  11. if (!/{/ && !/}/) {

  12. $number =@symbol;

  13. if ($number>0) {

  14. print " $_";

  15. } else {

  16. print $_;

  17. }

  18. }

  19.  

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”,一律换成“red

perl -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'
  1. 正则错误部分

正则替换部分,不能有正则 

$string =~ /-?(/d+)/.?(/d+)/;

$string =~ s/-?(\d+)/.$1/;

5 hash/引用

  1. #!/opt/exp/bin/perl -w

  2. #array init

  3. @a=();

  4. @LoL=();

  5.  
  6. sub function {

  7. $j=@_;

  8. @a=("1","2","3");

  9. print "j=$j\n";

  10. return @a;

  11. }

  12. for $i (1..10) {

  13. @list = function ($i);

  14. #below two is the same ,it is array reference

  15. $LoL[$i] = \@list;

  16. # it is the same with above code

  17. # $LoL[$i] = [ @list ];

  18. # it is array number

  19. $LoL[$i] = scalar @list;

  20. #notes add {} ,reback to array

  21. print "2:@{$LoL[$i]} \n";

  22. #print "2:$LoL[$i] \n";

  23. }

2:

  1. #!/opt/exp/bin/perl -w

  2. sub printSorted {

  3. ·#parameter1 is reference for array, parameter2 is hash.@_ list assign

  4. my ($data,%options) = @_;

  5. # get the value for array

  6. print "$data->[3]\n";

  7. #get hash value

  8. print "$options{type}\n";

  9. }

  10. #$list={"1","2","3","4"};

  11. @list=("1","2","3","4");

  12. printSorted (\@list,dir => 'asc',type => 'numerical');

3

  1. #!/opt/exp/bin/perl -w

  2. my @array1 = (10, 20, 30, 40, 50);

  3. my @array2 = ( 1, 2, \@array1, 3, 4);

  4. %hash=(abc=>123, def=>456);

  5. #reference to hash

  6. $ref=\%hash;

  7. #point to hash

  8. print "%$ref\n";

  9. print "$$ref{abc}\n";

  1. <span style="font-size:14px;">#!/opt/exp/bin/perl -w

  2. #it doesn't use $_ in hash and array

  3. #array

  4. #set value

  5. @list = (4,3,2,1,0);

  6. foreach $item (@list) {

  7. printf ("%d->$item\n",$i++)

  8. }

  9. #hash

  10. while (my ($key,$value) = each (%ENV)) {

  11. print "$key - $value\n";

  12. }

  13. my @val =sort keys %ENV;

  14. for ($i=0;$i<@val;$i++) {

  15. $key=$val[$i];

  16. print "$key-----$ENV{$key} \n";

  17. }</span>

==

在将hash的keys  sort 后,sort是一个独立的循环操作.先这个函数。
map是一个独立的循环,输出key和value,
即按比较顺序输出key-value

print map {"$_ -> $hash{$_}"} sort {$a cmp $b} keys %hash; 

应该是print if aa ; 这种单句处理
上面的sort是一个执行

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 data

chomp(@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/

你可能感兴趣的文章