perl study note-1

本文深入介绍了Perl中数组的基本概念及高级用法,包括定义、索引、长度、元素操作等,并通过实例展示了如何使用sort、join、split、map和grep等函数进行高效的数据处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

# !/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 
last   shift  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 /- 5   20   4 / ;
@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 / 25   55   69 / ;
@maparr   =   map  { sqrt ( $_ ) * 10 @maparr ;
print   " map: @maparr  " ;

#  grep
my   @greparr   =  qw /- 1   - 4   5   7 / ;
@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 );
        
last   if  ( $index < 0   ||   $index > 6 );
        
print   " index: $index   value: $arr[$index] " ;
}
print   " $_  "   for   sort  { $a <=> $b @arr print   " " ;
print   " elements greater than 40: $_ "   for   grep  { $_   >   40 @arr ;
print   " each elem divided by 10: $_ "   for    map  { $_   /   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 :   3   4   5   8
digital 
sort :   - 5   4   20  
string 
sort :   - 5   20   4  
digital 
sort ,   sort  by power  2 :   4   - 5   20  
join  the strings ,  seperated by  " : " ,  user : id : gid : passwd :
split   " I:am:lzc "  into  " I am lzc "   
map :   50   74.1619848709566   83.0662386291807  
grep ,   select  elems greater than  0 :   5
grep ,   select  elems greater than  0 :   7

part4
:  exercise
pls input 
index   0 ... 6 :   4
index :   4          value :   58
pls input 
index   0 ... 6 :   8
24   24   33   42   58   65   87  
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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值