perl的array和map/hash
2010-03-25 17:08
218 查看
一 array
1)实例
use strict;
use warnings;
my @myarray = (123,"hello", 456, 'guy');
foreach(@myarray)
{
print "$_ " ;
}
print "\n";
foreach my $item (@myarray)
{
print "$item " ;
}
print "\n";
for(my $i = 0; $i <scalar(@myarray); $i++)
{
print "$myarray[$i]" . " ";
}
print "\n";
for(0..($#myarray))
{
print "$myarray[$_]" . " ";
}
print "\n";
@myarray = (@myarray, 789);
print "@myarray\n" ;
push(@myarray,"gril");
print "@myarray\n" ;
unshift(@myarray, '000');
print "@myarray\n" ;
delete $myarray[1];
print "@myarray\n" ;
2)函数,如下:
![](http://images.cnblogs.com/cnblogs_com/itech/build/perl18.png)
3)注释:
1】使用@定义array,使用array的item时$array
;
2】使用scalar来获得array的size;
3】$#获得最大的index,即size-1;
4】$_在for和foreach中表示当前item;
5】push/pop用来在array的最后加入和弹出item;
6】shift/unshift用来在array的前面删除和插入item;
7】split/john用来实现array和string间的转化;
8】delete可以用来删除item,例如delete $myarray[1];
二 map/hash
1) 实例:
use strict;
use warnings;
my %myhash = ('k1',100,'k2',200);
# my %myhash = (k1=>100,k2=>200);
print %myhash ;
print "\n";
$myhash{'k3'} = 300;
$myhash{'k4'} = 400;
print %myhash ;
print "\n";
foreach my $key (keys %myhash)
{
print $key . " indexes ".$myhash{$key}."\n";
}
foreach my $value (values %myhash)
{
print $value ."\n";
}
while((my $key, my $value)= each(%myhash))
{
print "$key indexes $value \n"
}
if(exists $myhash{'k1'}) {print "k1 is exist\n";}
delete $myhash{'k1'};
print %myhash ;
print "\n" ;
2)函数,如下:
![](http://images.cnblogs.com/cnblogs_com/itech/build/perl19.png)
3)注释:
1】%用来定义map/hash;
2】对单个的key赋值是使用$,例如$myhash{'k3'} = 300;
3】keys获得所有的keys到array;
4】values获得所有的values到array;
5】迭代,每次返回一对key/value;
6】exists用来判断某个key是否存在;
7】delete用来删除指定的key,同时对应的value也被删除;
三 总
Array Functions@array = ( ); Defines an empty array@array = (“a”, “b”, “c”); Defines an array with values$array[0] The first element of @array$array[0] = a; Sets the first element of @array to a@array[3..5] Array slice - returns a list containing the 3rd thru 5thelements of @arrayscalar(@array) Returns the number of elements in @array$#array The index of the last element in @arraygrep(/pattern/, @array) Returns a list of the items in @array that matched/pattern/join(expr, @array) Joins @array into a single string separated by exprpush(@array, $var) Adds $var to @arraypop(@array) Removes last element of @array and returns itreverse(@array) Returns @array in reverse ordershift(@array) Removes first element of @array and returns itsort(@array) Returns alphabetically sorted @arraysort({$a<=>$b}, @array) Returns numerically sorted @array
Hash Functions%hash = ( ); Defines an empty hash%hash = (a => 1, b=>2); Defines a hash with values$hash{$key} The value referred to by this $key$hash{$key} = $value; Sets the value referred to by $keyexists $hash{$key} True if the key/value pair existsdelete $hash{$key} Deletes the key/value pair specified by $keykeys %hash Returns a list of the hash keysvalues %hash Returns a list of the hash values
完!
1)实例
use strict;
use warnings;
my @myarray = (123,"hello", 456, 'guy');
foreach(@myarray)
{
print "$_ " ;
}
print "\n";
foreach my $item (@myarray)
{
print "$item " ;
}
print "\n";
for(my $i = 0; $i <scalar(@myarray); $i++)
{
print "$myarray[$i]" . " ";
}
print "\n";
for(0..($#myarray))
{
print "$myarray[$_]" . " ";
}
print "\n";
@myarray = (@myarray, 789);
print "@myarray\n" ;
push(@myarray,"gril");
print "@myarray\n" ;
unshift(@myarray, '000');
print "@myarray\n" ;
delete $myarray[1];
print "@myarray\n" ;
2)函数,如下:
![](http://images.cnblogs.com/cnblogs_com/itech/build/perl18.png)
3)注释:
1】使用@定义array,使用array的item时$array
;
2】使用scalar来获得array的size;
3】$#获得最大的index,即size-1;
4】$_在for和foreach中表示当前item;
5】push/pop用来在array的最后加入和弹出item;
6】shift/unshift用来在array的前面删除和插入item;
7】split/john用来实现array和string间的转化;
8】delete可以用来删除item,例如delete $myarray[1];
二 map/hash
1) 实例:
use strict;
use warnings;
my %myhash = ('k1',100,'k2',200);
# my %myhash = (k1=>100,k2=>200);
print %myhash ;
print "\n";
$myhash{'k3'} = 300;
$myhash{'k4'} = 400;
print %myhash ;
print "\n";
foreach my $key (keys %myhash)
{
print $key . " indexes ".$myhash{$key}."\n";
}
foreach my $value (values %myhash)
{
print $value ."\n";
}
while((my $key, my $value)= each(%myhash))
{
print "$key indexes $value \n"
}
if(exists $myhash{'k1'}) {print "k1 is exist\n";}
delete $myhash{'k1'};
print %myhash ;
print "\n" ;
2)函数,如下:
![](http://images.cnblogs.com/cnblogs_com/itech/build/perl19.png)
3)注释:
1】%用来定义map/hash;
2】对单个的key赋值是使用$,例如$myhash{'k3'} = 300;
3】keys获得所有的keys到array;
4】values获得所有的values到array;
5】迭代,每次返回一对key/value;
6】exists用来判断某个key是否存在;
7】delete用来删除指定的key,同时对应的value也被删除;
三 总
Array Functions@array = ( ); Defines an empty array@array = (“a”, “b”, “c”); Defines an array with values$array[0] The first element of @array$array[0] = a; Sets the first element of @array to a@array[3..5] Array slice - returns a list containing the 3rd thru 5thelements of @arrayscalar(@array) Returns the number of elements in @array$#array The index of the last element in @arraygrep(/pattern/, @array) Returns a list of the items in @array that matched/pattern/join(expr, @array) Joins @array into a single string separated by exprpush(@array, $var) Adds $var to @arraypop(@array) Removes last element of @array and returns itreverse(@array) Returns @array in reverse ordershift(@array) Removes first element of @array and returns itsort(@array) Returns alphabetically sorted @arraysort({$a<=>$b}, @array) Returns numerically sorted @array
Hash Functions%hash = ( ); Defines an empty hash%hash = (a => 1, b=>2); Defines a hash with values$hash{$key} The value referred to by this $key$hash{$key} = $value; Sets the value referred to by $keyexists $hash{$key} True if the key/value pair existsdelete $hash{$key} Deletes the key/value pair specified by $keykeys %hash Returns a list of the hash keysvalues %hash Returns a list of the hash values
完!
相关文章推荐
- perl的array和map/hash
- perl hash 和 map 新理解
- perl push an array to hash
- Perl 中输出 hash 和 array
- stl之hash_map
- [转]NHibernate集合映射中的set, list, map, bag, array
- Copy On Write Hash Map
- 巧用array_map()和array_reduce()替代foreach循环
- Hibernate 多表关联映射- Hibernate中使用的集合类型(set,list,array,bag,map)
- perl hash 根据键访问相应值
- php中的array_map,array_walk以及匿名函数
- Array,Collection,Map的浅析
- grails - How to check if element in groovy array/hash/collection/list? - Stack Overflow
- How do I sort an array of hashes by a value in the hash?
- 自定义类型使用hash_map时自定义hash函数
- set/multiset map/multimap hash_set/hash_multiset hash_map/hash_multimap 区别与联系
- map hash_map unordered_map 性能测试
- Perl HASH数组
- STL hash_map使用
- map, hash_map,unordered_map