Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Summary of perl programming

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

Perl has three main data types, one is a variable saved by $, one is an array saved by @, and the other is a hash array saved by%.

The variable $in perl

Saved variables are indistinguishable from any type in PERL, that is, there is no concept of data types. Perl will automatically recognize it according to the context. For example, my $value = 10 position Perl will be identified as a variable. Concatenating two string variables in perl uses. No. For example

My $first = "I love"; my $second = "study"; my $value = $first.$second; concatenates the two strings.

Compare the size of numbers in perl >

< =等,字符串比较大小是 eq gt等。 对字符串的的替换使用的命令是 s///; 例如 my $test = " it "; $test = ~ s/^\s+//;$test =~ s/\s+$//;去掉首尾的空格,这对于文件路径很重要,去掉开头与结尾的空格。符号^表示是字符串的开头,符号$表示是符号的结尾。.匹配任何字符,*表示是0到多个,+表示一个到多个,?表示0到1一个。 split命令 使用split命令可以将一个字符串分解成一个数组。使用方式为 split /swparator/,$value;例如 my $test = "it ,is ,goood"; my @array = split/,/$testl;得到的结果数组中就是it is goood. join命令 join命令与split的命令相反,是把数组中的每一个变量粘合在一起。用法 join $value,@array.上例子中,使用join例如 my $value = join ,@array. 最后$value 为"it,is,gooood"; m//; 将变量按照一定的模式进行匹配m//之间可以是正则表达式也可以是一定变量,例如:my $test = "how are you"; if($test =~ m/test/) 看字符串是否包含test。 $_ 代表目前perl处理的变量,chomp($value);可以去掉输入的变量后面的回车符号。 \ 这个字符代表获得元字符,是获得元字符的意思。例如想在正则表达式中匹配点号.使用\.就可以防止perl程序进行转意。 index命令 在一个字符串中查找一个子串第一次出现的位置, my $test = "you are good"; my $place = index($test,"are");就是查找字符串are第一次出现的位置,rindex是查找指定字符串最后一次出现的位置。 substr命令 取一个字符串的子串,例如 my $test = "you are good"; my $newstr = substr($test,0,3); $newstr就为you. die命令 die可以使perl遇到错误时跳出程序。 perl中的数组@ perl中像数组中插入元素为 push @test,$value;弹出元素是 my $test = pop @test;在最前面取和存分别是my $test = shift @test; unshift @test,$value;判断数据大小为 my $size = @array;取得数组的大小。 if(@array ~~$value)数组中是否包含这个元素。清空数组中的元素为undef(@array); 多维数组插入方式为push @AllSheetContent,[@AllRowDate];获取方式为$AllSheetContent[0]获得的是第一维数组。若是 push @AllSheetContent,$value1; push @AllSheetContent,$value2; $AllSheetContent[0]取得的是$value1; perl中的哈希% 哈系声明,my %hashtable; 哈系赋值 $hashtable{$key} = $value;注意哈系是大括号,数组取值是中括号。获得哈系的所有key值为 my @key = keys %hashtable;获得哈系的所有values值为 my @value = values %hashtable; 获取哈系数目的大小 my $count = keys %hashtable; 判断哈系是否包含某个key值为 if(exists $hashtable($value)) 删除哈系中的一个键值对为 delete $hashtable{$value} perl中的文件操作 文件句柄 open openhandle "filename"; open openhandle ">

> filename "

The three ways to open a file are: the first is to open the file for read operation. The second is to open the file as a write, and delete it if a file with the same name exists before. The third is to open the file as an append write, or to append it after the file if it existed before. Finally, remember to close the file handle. Close (openhandle)

If (- e $filename) is used to determine whether a file exists, if (- d $filename) is used to determine whether a file is a directory, and if (- f $filename) is used to determine whether a file is a file.

Basically that's it, it's a little messy, and what I know is being added later.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report