数组:pop&清空数组&查找某元素是否在数组内

pop操作将数组的最后一个元素取出并返回:

@array=5..9;



$fred=pop(@array);#$fred得到9,@array现在为(5,6,7,8)



$barney=pop@array;#$barneygets8,@array现在为(5,6,7)



pop  @array;#@array现在为(5,6)(7被丢弃了)

 

数组清空:

@=();

查找某元素是否在数组内:

比如我有一个list
@ifs=(eth1 eth2.45 eth3);
然后有个变量
$iface="eth2";
我要判断$iface在不在@ifs里

 

if ( grep { $_ eq $iface } @ifs )



{ # 存在 }

 

或是:

#!/usr/bin/perl

@ifs=qw(eth1 eth2.45 eth3);

$it=grep /$eth2/, @ifs;

if($it==0){

print "NO\n";

}

else{

print "YES\n";

}

 

 

 

你可能感兴趣的:(数组)