关于数组的一些用法

来源:互联网 发布:淘宝专卖 编辑:程序博客网 时间:2024/05/16 04:43
下面的代码演示了关于数组的一些用法:     
$s="how,are,you";
@array
=split(/,/,$s);
$length
=@array;
($t)
=@array;

printf 
"The first element is %s ",$t;
printf 
"The first element is %s ",@array;
printf 
"The length of array is %d ",$length;

print 
"the elements are: ";
for($i=0;$i<$length;$i++)
{
    printf 
"The $i is $array[$i] ";
}     
 
运行结果如下:
The first element is how
The first element 
is how
The length of array 
is 3
the elements are: 
The 
0 is how
The 
1 is are
The 
2 is you

若把上面代码中的循环部分修改如下,亦可达到输出各元素的效果:

for($i=0;$i<@array;$i++)
{
    printf 
"The $i is $array[$i] ";
}



原创粉丝点击