#!/usr/bin/perl -w use strict; # two ways to print meta-char
# qq: qq can be followed by any symbol pairs, such as: //,||,{} print"$ @ "; print qq|$ @ n|; =head1
input can also receive the "" char chomp only can delete the last"" char and return the count of "" deleted
results:
pls input the value: myinput delete number of n is 1
myinput =cut print" part1: inputs "; print"pls input the value: "; my$in=<STDIN>; $in.=""; # .= used to combine two strings print"delete number of /n is ",chomp($in),""; print$in; =head2
part2:undef&defined
we can define a var as "my $myvar;" with no initialization, perl treat it as "undef"; myvar is 0 when it is treated as digital,while it is a null("")
string when it is treated as string;
perl will show a warning when you open warnings option and print a undef var; use"print "$myvar" if (defined $myvar);"for a check before print; =cut print" part2: undef & defined "; my$myvar; my$myvar1=undef; print"$myvar"if (defined$myvar); print"print a undef var",$myvar,$myvar1,""; =head3
part3: array ### basic defined:my@array; my ($arr[0],$arr[1],$arr[2]) = qw/first second third/; my@array= qw/first second third/; index: $#array is the last index of array; length:length of array is ($#array+1); we can skip middle elements and insert a new value, which will extend the array with the middle elements undef;
also,"$length = @array;", perl handle the operation according to contex; ### get elements from array push/pop:push a new value, especially an existing array, to the end of array; pop one value from the end of array, LIFO and the length=length-1; shift/unshift: operate the elements from the head of array, it can lead to the shift of the elements, this is, after "shift", element from 2nd
to the lastshift forward one space; split: get continuous values from array ### sort sort:@array=sort { [$a<=>$b] | [$a cmp $b] } @array "<=>" used for digital while"cmp" used for string; $a,$b are reserved for compare ### join/split join: combine a series of strings into one string in some special way; join'some commas', [@array] | qw// split:split a string in some special way into small ones to an array split'some commas', [@array] | qw// ### map map: get element one by one from the array or series, then do some operation,
finally, store the results into another array; map { operation of "$_" } [@array]|[qw/.../] ### grep grep:select elements corresponding to some condition from array or series; grep { $_ correspond to some condition} [@array]|[qw//] =cut print" part3: array "; my@arr= (0,1,2,3...9); my@arr1= (-1,@arr,10); my@arr2= qw/30@arr/; my@arr3= qw{1st 2nd}; # like qq $arr3[3] ='4th'; my$len=@arr3; print'use $#array: length of arr3 is ', $#arr3+1, " "; print'use len=@array: length of arr3 is ',$len,""; print"undef arr3[2] is $arr3[2] "; # leave $arr3[2] undefined
print $arr[-1]; # counting backwards, -1 point to the last element;
# pop print"pop a value from array: ",pop@arr,""; push@arr,100; push@arr2,@arr1; # pop VS. shift print"before pop, length is ", $#arr+1, " "; pop @arr; print"after pop, length is ", $#arr+1, " "; print"before shift, length is ", $#arr+1, " "; shift @arr; print"after shift, length is ", $#arr+1, " ";
# get sub-elems @arr= (0...10); my@splitarr=@arr[3...5,8]; print"split: get 3...5 and 8th elements: @splitarr "; # sort my@sortarr= qw/-5204/; @sortarr=sort {$a<=>$b} @sortarr; print"digital sort: @sortarr "; @sortarr=sort {$a cmp $b} @sortarr; print"string sort: @sortarr "; @sortarr=sort {($a**2) <=> ($b**2)} @sortarr; print"digital sort, sort by power 2: @sortarr "; # join/split my@joinarr= qw|user id gid passwd|; print'join the strings, seperated by ":", ',join':',@joinarr,""; my$splitstr="I:am:lzc"; @joinarr=split':',$splitstr; print"split "$splitstr" into "@joinarr" "; # map my@maparr= qw/255569/; @maparr=map {sqrt($_)*10} @maparr; print"map: @maparr "; # grep my@greparr= qw/-1-457/; @greparr=grep {$_>0} @greparr; print"grep, select elems greater than 0: $_ "for@greparr; =head4
part4: exercise 1. put (24,33,65,42,58,24,87) into an array; 2. prompt user to input the index and print the value; 3.sort the array, and print it 4.print all elements which are greater than 40; 5.each elem in array divided by 10,print results =cut print" part4: exercise "; my$index=0; @arr= (24,33,65,42,58,24,87); while (1)
{ print"pls input index 0...6: "; $index=<STDIN>;chomp($index); lastif ($index<0||$index>6); print"index: $index value: $arr[$index] ";
} print"$_ "forsort {$a<=>$b} @arr; print""; print"elements greater than 40: $_ "forgrep {$_>40} @arr; print"each elem divided by 10: $_ "formap {$_/10} @arr;
[root@china1 code]# ./perlnote2.pl $ @
$ @
part1: inputs
pls input the value:2 delete number of n is 1 2
part2:undef&defined Use of uninitialized value in print at ./perlnote2.pl line 37,<STDIN> line 1. Use of uninitialized value in print at ./perlnote2.pl line 37,<STDIN> line 1. print a undef var
part3: array use $#array: length of arr3 is 4 use len=@array:length of arr3 is 4 Use of uninitialized value in concatenation (.) or string at ./perlnote2.pl line 85,<STDIN> line 1. undef arr3[2] is pop a value from array:9
before pop,length is 10
after pop,length is 9
before shift,length is 9
after shift,length is 8 split: get 3...5 and 8th elements:3458
digital sort:-5420
string sort:-5204
digital sort,sort by power 2:4-520 join the strings, seperated by ":", user:id:gid:passwd: split"I:am:lzc" into "I am lzc" map:5074.161984870956683.0662386291807 grep,select elems greater than 0:5 grep,select elems greater than 0:7
part4: exercise
pls input index0...6:4 index:4 value:58
pls input index0...6:8 24243342586587
elements greater than 40:65
elements greater than 40:42
elements greater than 40:58
elements greater than 40:87 each elem divided by 10:2.4 each elem divided by 10:3.3 each elem divided by 10:6.5 each elem divided by 10:4.2 each elem divided by 10:5.8 each elem divided by 10:2.4 each elem divided by 10:8.7