|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291 |
perl重点基础知识整理#若无特殊说明,以下都是基于ubuntu 13.04版本1:终端下运行perl脚本:step1:在桌面新建一个perl脚本,输入:#! /usr/bin/perlprint
"hello world!";step2:打开终端:hqh@ubuntu:~$ cd ~/桌面hqh@ubuntu:~/桌面$ sudo chmod
a+x h.plhqh@ubuntu:~/桌面$ ./h.plhello world!hqh@ubuntu:~/桌面$看到输出: hello world!2:模块的安装:http://search.cpan.org/ 搜到模块然后下载至 /home/hqh/下载 cd ~/下载tar zxvf URL-Signature-0.03.tar.gzcd cd URL-Signature-0.03/perl Makefile.PLmakemake install3:defined,undef,exists函数:#! /usr/bin/perlmy $cin=undef;if (defined($cin)){ print
"The input was $cin";}else{ print
"No input available! \n";} my %hash1=("a"=>1,"b"=>2,"c"=>3);print
exists($hash1{"a"}),"\n";print
exists($hash1{"d"}),"\n";print
defined($hash1{"a"})."\n";说明:exists主要针对hash,判断是否存在key如果一个变量为undef,则definded判断该变量为0,其他情况为14:hash解析#! /usr/bin/perl#hash几种解析方式my %hash1=(‘a‘,2,‘b‘,3);while(my($k,$v)=each%hash1){ print
"$k--->$v"; print
"\n";}#hash长度my $len=scalar
keys%hash1;print
$len,"\n";foreach
my $key1(keys
%hash1){ print
$key1,"-->",$hash1{$key1}; print
"\n";}5:深入理解 @_以及$_@_:默认的参数数组$_:默认的变量自己写了一段下面的程序,清晰了!!嘿嘿#! /usr/bin/perlsub myfun2{ A1: for(my
$i=0;$i<=4;$i++){ print
$_[$i],"\n"; } my
$str1=shift
@_; print
@_,"\n"; print
scalar @_,"\n"; my
($str2,$str3)=@_[2,3]; print
"my str1 is $str1 \n"; print
"str2 is $str2 and str3 is $str3"; foreach
my $keys(@_){ print
"key analysis",$keys,"\n"; } A2: for(my
$i=0;$i<=3;$i++){ print
$_[$i],"\n"; }}print
&myfun2(1,2,3,4,5),"\n";调用sub程序,将1到5默认复制给@_,shift
@_,取出@_数组的第一个元素1,并且@_自动更新为(2,3,4,5);将@_的第3,4个元素复制给str2,str3遍历数组$_表示默认的变量,可以看出,变量的个数与@_的长度一样,看A1和A2两段for循环就可以知道,随着@_做了shift后,长度发生变化,而$_的个数也随之变化6:index,rindex,split函数#! /usr/bin/perlprint
$url,"\n",length($url),"\n";print
index($url,"www"),"\n";print
index($url,"www",8),"\n";print
index($url,"."),"\n";print
rindex($url,"."),"\n";my @list=split(/\./,$url);print
@list;print
scalar @list,"\n";my @list1=split(/\\\\/,$url);print
@list1;7:高级排序---针对hash哦my %score = ("barney"=>100, "fred"=>205, "dino"=> 30,"yellow"=>100);my @winners = sort
by_score keys
%score;sub
by_score{ $score{$b}<=>$score{$a}}print
@winners;sub
by_score_or_name{ $score{$b}<=>$score{$a} or $a
cmp $b; } my @winners1 = sort
by_score_or_name keys
%score;print
@winners1; 8:之前的学习笔记#! /usr/bin/perlmy $str="huangqihao";print
$str x 4;print
"$str \n" x 4;print
5 x 4;print
"\n";print
2.5%2;print
"\n";print
2.5**7;print
"\n";#交换两个变量($a,$b)=($b,$a);print
$a,$b;print
"\n";#qw符号($a,$b,$c)=qw !abc 123 434!;print
"$a\n$b\n$c\n";($a,$b,$c)=qw .abc 123 434.;print
"$a\n$b\n$c\n";($a,$b,$c)=qw :abc 123 434:;print
"$a\n$b\n$c\n";#pop,push@www=(‘ab‘,‘cd‘,‘ef‘);print
pop(@www);push(@www,0);print
@www;#pop 和 push的第一个参数都为数组变量#shift,unshift@www=(‘ab‘,‘cd‘,‘ef‘);print
shift(@www);unshift(@www,4);print
@www;#shift和 unshift的第一个参数都为数组变量#数组插入字符串$ii="sjdfk@www sdjfkl";print
$ii;#reverse,sort@www=(‘db‘,‘cd‘,‘ec‘);print
reverse(@www);print
sort(@www);print
"\n";# #读文件open(FD,"/home/hqh/桌面/2.pl")||die("can not open the file");@aa=<FD>;print
@aa;foreach
(@aa){ print
"$_\n";}#写文件open(HD,">>/home/hqh/桌面/3.pl")||die("can not open the file");#HD为写入句柄print
HD "@aa";open(ND,"/home/hqh/桌面/3.pl")||die("cant not open the file");@bb=<ND>;print
@bb;#追加文件open(ZJ,">>/home/hqh/桌面/3.pl")||die("can not open the file");print
ZJ "@aa";open(ZJ,"/home/hqh/桌面/3.pl")||die("can not open the file");@cc=<ZJ>;print
@cc;#关注循环体中last,next,redo的操作!!!$sum1=0;for($i=0;$i<=10;$i++){ $sum1=$sum1+$i; last
if($i==6);}print
"sum1=$sum1\n";##只加到5,遇到6,就跳出循环啦!!$sum2=0;for($i=0;$i<=10;$i++){ next
if($i==6); $sum2=$sum2+$i;}print
"sum2=$sum2\n";#######6木有加进去,其他都加进去了$sum3=0;for($i=0;$i<=10;$i++){ print
"hello,redo is here!\n"; redo
if($i==6); $sum3=$sum3+1;}print
"sum3=$sum3\n";############一直在循环print hello..#三元操作符,等于R语言中ifelse函数$a=1+1>3 ? " larger"
:" smaller";print
$a;#三元操作符,等于R语言中ifelse函数$a=1+1>3 ? " larger"
:" smaller";print
$a;#perl目录文件操作chdir
"/home/hqh/桌面/perlperl" or die
"cannot chdir to /etc: $!" ;@fileparameters=glob"*";print
@fileparameters;chdir
"/home/hqh/桌面/perlperl/first perl" or die "cannot chdir to /etc: $!" ;@fileparameters=glob"*";print
@fileparameters;chdir
"/home/hqh/桌面/perlperl" or die
"cannot chdir to /etc: $!" ;#解析目录opendir(HD ,"/etc") || die
"can not opendir! ";@aa=readdir(HD);print
@aa;foreach
$tt(@aa){ print
"$tt\n";}closedir
HD;#删除操作 unlink 与glob结合#unlike glob "*.pm";#重命名 rename#创建和删除目录 mkdir rmdir#修改权限 chmod#改变所有者 chown 1004 ,100,glob "*.pm" ,1004表示user的ID,调用 getpwnam 函数,将名字转换为数字,而对应的 getgrnam#将组名转换为数字#index函数,查找子串在主串中的位置!#substr函数#sprintf函数,返回请求的字符串,不被打印出来,用于赋值,比较豪! |
原文地址:http://www.cnblogs.com/datacatcher/p/3761675.html