Perl学习笔记2:数组与Hash的操作

这是Per学习笔记的第二部分

#!/usr/bin/perl
#Perl 学习资料  //注释的写法

print "=================================================\n";
print "数组\n";
print "=================================================\n";

#数组的初始化
@MY_ARAY_001=(1..12);
@MY_ARAY_002=('WJK','HM','ZJL');
print "@MY_ARAY_001\n";
print "@MY_ARAY_002\n";

#直接把数组赋予变量,获得数组维度
$SIZE = @MY_ARAY_002;
print "MY_ARAY_001 Size : $SIZE \n";

#数组后方加入数据
push (@MY_ARAY_001,15);
print "push MY_ARAY_001 : @MY_ARAY_001 \n";
#数组前方加入数据
unshift (@MY_ARAY_001,-14);
print "unshift MY_ARAY_001 : @MY_ARAY_001 \n";
#数组后方删除数据
pop (@MY_ARAY_001);
print "pop MY_ARAY_001 : @MY_ARAY_001 \n";
#数组前方删除数据
shift (@MY_ARAY_001);
print "shift MY_ARAY_001 : @MY_ARAY_001 \n";

#选取数组中特定元素到新数组
@[email protected]_ARAY_001[1,3,5,7,9,11];
print "MY_ARAY_003 : @MY_ARAY_003 \n";
#替换数组中特定元素,从新数组
splice (@MY_ARAY_001,1,4,@MY_ARAY_002);
print "splice (MY_ARAY_001,1,4,MY_ARAY_002) : @MY_ARAY_001 \n";
#数组转字符
$STR1=join('&',@MY_ARAY_001);
print "Array2Str : $STR1 \n";
#字符转数组
@MY_ARAY_005=split('&',$STR1);
print "Str2Array : @MY_ARAY_005 \n";
#数组排序
@MY_ARAY_005=sort(@MY_ARAY_005);
print "Sorted : @MY_ARAY_005 \n";

print "=================================================\n";
print "哈希表\n";
print "=================================================\n";
#HASH的初始化
%MY_HASH_001=("Key1"=>"HM","Key2"=>"WJK","Key3"=>"ZJL","Key4"=>"GY");
%MY_HASH_002=(1=>"HM",2=>"WJK",3=>"ZJL",4=>"GY");
print "Key2's Value : $MY_HASH_001{'Key2'} \n";

@MY_ARAY_001=keys %MY_HASH_001;			#从HASH里获得所有的Key到数组
@MY_ARAY_002=values %MY_HASH_001;		#从HASH里获得所有的Value到数组
print "Str2Array : @MY_ARAY_001 \n";
print "Str2Array : @MY_ARAY_002 \n";

$SIZE = @MY_ARAY_001;
print "MY_HASH_001's Size : $SIZE \n";		#从HASH到数组,然后获取SIZE

@MY_ARAY_002= @MY_HASH_002{2..3};		#从HASH里取得若干Value
print "MY_ARAY_002 : @MY_ARAY_002 \n";

#HASH,元素存在
if(exists($MY_HASH_002{1})){
	print "MY_ARAY_002{1} exists: $MY_HASH_002{1} \n";
}