一些关于数组的练习(1)
时间:2005-04-26 来源:寂寞烈火
1,得到数组长度:
/home/lee#perl -e '@array=qw(a b c);print "length:",$#array+1," "'
length:3
$#array是数组的下标,由于数组的下标从0开始,那么数组长度就是最大下标加1即可得到数组程度!
2,清空数组的方法:
$#array=-1或者@array=()
3,合并和附加数组:
/home/lee#perl -e '@a=(1..3);@b=(4..6);@c=(@a,@b);print join(" ",@c)," "'
1 2 3 4 5 6
通过表赋值的方法,将数组a和b合并为一个数组c
也可用push函数合并数组
/home/lee#perl -e '@a=(1..3);@b=(4..6);push @a,@b;print join(" ",@a)," "'
1 2 3 4 5 6
4,数组片技术:
数组片是数组的一个部分,可以通过方括号中放置数组下标号来指明想要把哪些元素放到片中,如:
/home/lee#perl -e '@a=(1..10);@b=@a[3..6];print join(" ",@a," ",@b)," "'
1 2 3 4 5 6 7 8 9 10
4 5 6 7
把数组a中3到6号元素给新数组b
一个典型的数组片的例子:
/home/lee#perl -
($atime,$mtime)=(stat "filename")[8,9];
print scalar localtime $atime," ",scalar localtime $mtime," "
__END__
Sat Apr 23 14:14:04 2005
Wed Apr 20 00:38:51 2005
打印stat函数返回文件filename的最后访问时间和最后修改时间
OK,continue...