- 论坛徽章:
- 0
|
请教:substring在string中的重复次数
原帖由 "superdoctor" 发表:
"($repeat==length($substr)?2:1)
superdoctor兄理解偏了,我应当这么举例:
me,mememe =>;type为2,repeat为3
we,wewewewewe=>;type为2,但repeat为5
hi,hihihi=>;type为2,repeat为3
me行和hi行type与repeat都相等,合并,计数count为2;
(me行的type=hi行的type)&(me行的repeat=hi行的repeat)
怒剑狂啸兄的代码正是我期望的, hash应该是好的解决方案.又如
原帖由 "chaoslawful(perlChina) " 发表:
既然要求合并type和repeat都相等的行,那就可以把type和repeat值合起来作为散列键,自己用分隔符串联或者用perl的multidimensional array emulation都可以
while(chomp($line=<> ) {
@fields=split(/,/,$line,2);
$stats{length($fields[0]),$fields[1]=~s/$fields[0]/$fields[0]/g}++;
}
foreach (keys %stats) {
print join(",",split($; ,$_),$stats{$_}),"\n";
}
呵呵,都是好手啊!多谢诸位! |
|