寒假【perl语言学习脚本】6.1 6.2 6.3

在这里插入图片描述
#!/usr/bin/perl -w
my %gf_name = (
dongzhu => ‘yang’,#=>胖箭头
lin => ‘zhou’,
fangfang => ‘zhang’,
);
print “please input the name:\n”;
chomp(my $person= );
if (exists KaTeX parse error: Expected '}', got 'EOF' at end of input: gf_name{person}){
print “hey,$person exists there\n”;
}
#用exists函数检查哈希的键是否存在
print “her name is KaTeX parse error: Expected '}', got 'EOF' at end of input: gf_name{person} KaTeX parse error: Expected 'EOF', got '\n' at position 9: person .\̲n̲"; #哈希就是一个键值对的形…word);#声明变量
chomp(@words=);
foreach $word(@words){
KaTeX parse error: Expected '}', got 'EOF' at end of input: count{word}+=1;#KaTeX parse error: Expected '}', got 'EOF' at end of input: count{word}=KaTeX parse error: Expected '}', got 'EOF' at end of input: count{word}+1
}
foreach $word (sort keys %count){ #my c o u n t = k e y s p r i n t " count =keys %hash;得到3,也就是说有三对键值 p125 sort根据ASCII编码排序 print " count=keysprint"word was seen KaTeX parse error: Expected '}', got 'EOF' at end of input: count{word} times.\n”;
}
#foreach循环会逐项处理各个单词,它会将KaTeX parse error: Expected '}', got 'EOF' at end of input: count{word}的值加上1,然后再存回KaTeX parse error: Expected '}', got 'EOF' at end of input: count{word}
#在第一份foreach循环里,每次出现的单词都会让KaTeX parse error: Expected '}', got 'EOF' at end of input: count{word}的值加1
#假设第一个单词是fred,那么 c o u n t " f r e d " 的 值 就 会 加 1 , 既 然 是 第 一 次 用 到 count{"fred"}的值就会加1,既然是第一次用到 count"fred"1count{“fred”},它的值自然是undef
#不过,因为我们将它当成数字来用(利用数字操作符+=或是较长写法的+)
#所以perl会自动把undef转换为0,相加的总和为1,所以会将1存回$count{“fred”}

在这里插入图片描述
#!/usr/bin/perl -w
my $longest =0;
foreach my $key(keys %ENV){ #帮助确定第一列的宽度
my k e y l e n g t h = l e n g t h ( key_length=length( keylength=length(key);
l o n g e s t = longest= longest=key_length if k e y l e n g t h > key_length> keylength>longest;
}#帮助确定第一列的宽度

foreach my k e y ( s o r t k e y s p r i n t f " key(sort keys %ENV){#依次处理hash,对键值进行排序 printf "%- key(sortkeysprintf"{longest}s %s\n", k e y , key, key,ENV{$key};#宽度字符是负的,是让字符串左对齐,一一对应
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值