perl学习笔记

1. 数组

#!/usr/bin/perl
my $str = "hello,nihao,no,o,good";
my @arr = split(/,/, $str);
print "len:" . @arr . "
";
for (my $i=0; $i < @arr; $i++)
{ #括号必须有
    print @arr[$i] . "
";
}

#!/usr/bin/perl
@ifs=qw(eth1 eth2.45 eth3);
$it=grep /eth2$/, @ifs;
if($it==0){
print "NO ";
}
else{
print "YES ";
}

2. 编码

 参考:http://bbs.chinaunix.net/thread-1751048-1-1.html

Perl 的 utf8 与编码处理
  本文基于笔者查阅的 perldoc 和试验结果。鉴于经常有人被编码问题困扰,现笔者把所了解的关于 Perl utf8 与编码处理的资料总结一下。由于所知有限,可能会有讲错的地方,如有需要,可自行查阅:
perldoc Encode
perldoc PerlIO
perldoc utf8
perldoc encoding

下面用到这几个函数,能提供有用的帮助:
Encode::is_utf8($str) # 当字符串 utf8 flag 为on时,返回真,否则返为假
utf8::is_utf8($str) # 同上
Encode::_utf8_on($str) # 手工设置 utf8 flag 为on
Encode::_utf8_off($str) # 手工设置 utf8 flag 为off
PerlIO::get_layers(FP) # 得到语柄的 layers

3. my $len = length($taginfo);

4.  if ($catname ne $tagname)

http://www.cbi.pku.edu.cn/chinese/documents/perl/perl3.htm#四、逻辑操作符

my $a = 4;
if ($a == 4)
{
    print "a == 4
";
}
if ($a != 3)
{
    print "a != 3
";
}

5. open(GO,">>text.txt")用追加,不要用>,>会清空原来的文件,进行从写,>>是追加

open(MYFILE, ">info");
foreach my $key (keys %newinfo)
{
        my $value = $newinfo{$key};    #index
        my $tagids = "";
        if(exists($newinfo_tagid{$key}))
        {
            $tagids = $newinfo_tagid{$key};
        }
        print MYFILE $value . "_" . $cat . "	$key	$addtag , $tagids
";
}
close(MYFILE);

6. my @arrs = split(/|/, $key);

字典 数组 长度

#取数组的长度可以用
$i = @arr;#$i即可获取到数组的长度
或者直接用scalar(@arr)获取数组的长度
$#typeArr可以返回typeArr数组的最后一个元素的下标,比scalar(@arr)少1

 scalar(keys %hash)

原文地址:https://www.cnblogs.com/kaituorensheng/p/4026582.html