perl的一些注意事项

perl的一些注意事项

perl数组

使用split()将数组转换字符串时,如需使用.,要加上转义字符\:

 1 $var_test = "runoob";
  2 @test = split('', $var_test);
  3 print "\@test: @test\n";
  4 
  5 ### 加上\!
  6 $var_string = "www\.baidu\.com";
  7 @string = split('\.', $var_string);
  8 #@string = split('.', $var_string);
  9 #@string = split("\.", $var_string);
 10 #@string = split(".", $var_string);
 11 
 12 print "\@string: @string\n";
 13 
 14 $var_names = "aaa, bbbb, ccc, ddd";
 15 @names = split(',', $var_names);
 16 print "\@names: @names\n";

但是对于join():

  1 @array = (111, 222, 333);
  2 
  3 ## 4种方式都可以
  4 #$str1 = join('.', @array);
  5 #$str1 = join(".", @array);
  6 #$str1 = join("\.", @array);
  7 $str1 = join(".", @array);
  8 print "$str1\n";
  9 
 10 @array2 = ("google", "abc", "Meta");
 11 $str2 = join(',', @array2);
 12 print "$str2\n";                    

perl哈希

读取hash所有的键:keys
读取hash所有的值:values
存在顺序与原本hash中的键值对顺序不一致的问题:

  1 %data = ('google' => 'google.com', 'runoob' => 'runoob.com', 'taobao' => 'taobao.cn');
  2 
  3 @all_keys = values(%data);
  4 
  5 print "$all_keys[0]\n";
  6 print "$all_keys[1]\n";
  7 print "$all_keys[2]\n";
################
ygy@ubuntu:~/tmp/perl_test$ perl values.pl 
taobao.cn
runoob.com
google.com
ygy@ubuntu:~/tmp/perl_test$ perl values.pl 
google.com
runoob.com
taobao.cn

你可能感兴趣的:(Programming,language,perl,scala,开发语言)