免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2659 | 回复: 9
打印 上一主题 下一主题

请教:substring在string中的重复次数 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-05-12 17:02 |只看该作者 |倒序浏览
in.txt为:
I,IIII
me,meme
we,wewewewewe
you,youyouyou
hi,hihi
awe,aweaweawe
期望out.txt为:
type repeat count
1,4,1
2,2,2
2,5,1
3,3,2

以me,meme这一行为例说明:me为substring,meme为string;
type等于length(substring),
repeat等于substring在string中的重复次数,
type与repeat都相等的合并,并计数count.

哪位大侠指点一下?代码说话最佳.

论坛徽章:
0
2 [报告]
发表于 2005-05-12 17:36 |只看该作者

请教:substring在string中的重复次数

COUNT 的计算我无法找到规律啊??

type与repeat都相等的合并,并计数count. ---什么意思?

论坛徽章:
0
3 [报告]
发表于 2005-05-12 21:32 |只看该作者

请教:substring在string中的重复次数

me,meme =>;type为2,repeat为2
we,wewewewewe=>;type为2,但repeat为5
hi,hihi =>;type为2,repeat为2
me行和hi行type与repeat都相等,合并,计数count为2;
we行count则计为1

论坛徽章:
0
4 [报告]
发表于 2005-05-13 08:37 |只看该作者

请教:substring在string中的重复次数

  1. open (FILE,"in.txt");
  2. while (chomp($_=<FILE>;)) {
  3.         ($a,$b)=split(/,/,$_);
  4.         $count=$b=~s/$a/$a/g;
  5.         $hash{eval{length$a}.",".$count}++;
  6. }
  7. close FILE;
  8. open (FILE,">;out.txt");
  9. print FILE "$_,$hash{$_}\n" foreach (sort keys%hash);
  10. close FILE;
复制代码

论坛徽章:
0
5 [报告]
发表于 2005-05-13 11:13 |只看该作者

请教:substring在string中的重复次数

原帖由 "biomasm" 发表:
me,meme =>;type为2,repeat为2
we,wewewewewe=>;type为2,但repeat为5
hi,hihi =>;type为2,repeat为2
me行和hi行type与repeat都相等,合并,计数count为2;
we行count则计为1



你楼顶的累记两字让我困惑了。。。

use strict;

my ($dest,$substr,$i,$n,$repeat);
while(1) {
$i=$n=$repeat=0;
print "enter destination chars: ";
chomp($dest=<STDIN>;
print "enter substring: ";
chomp($substr=<STDIN>;

while(($i=index($dest,$substr,$n))+1) {
   $n=$i+length($substr);
   $repeat++;
}

print "type\trepeat\tcount\n".length($substr)."\t".$repeat."\t".($repeat==length($substr)?2:1)."\n";
print "\n\nTry again===============\n";
}
exit 0;

无须HASH,每个字串仅循环一遍,应该说效率还可以罢,至于文本的到入倒出自己加上就可以了,我使用标准输入是为了便于测试。。。

今天cu是不是有点问题,特别漫

论坛徽章:
0
6 [报告]
发表于 2005-05-13 15:32 |只看该作者

请教: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";
}

呵呵,都是好手啊!多谢诸位!

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
7 [报告]
发表于 2005-05-14 02:40 |只看该作者

请教:substring在string中的重复次数

来一个shell的

  1. /home/lee#cat tmp
  2. I,IIII
  3. me,meme
  4. we,wewewewewe
  5. you,youyouyou
  6. hi,hihi
  7. awe,aweaweawe
  8. /home/lee#while read line;do
  9. >; n=$(echo $line|cut -f1 -d',')
  10. >; expr length $(echo $line|cut -f1 -d',')
  11. >; echo $line|cut -f2 -d','|awk -F"$n" '{print NF-1}'
  12. >; done<tmp|xargs -n2|sort -n|uniq -c|awk '{print $2","$3","$1}'>;out.txt;cat out.txt
  13. 1,4,1
  14. 2,2,2
  15. 2,5,1
  16. 3,3,2
复制代码

论坛徽章:
0
8 [报告]
发表于 2005-05-16 09:02 |只看该作者

请教:substring在string中的重复次数

寂寞烈火真是牛人啊,怎么能记得住那么多命令和参数,并将它们组合运用的啊?极度崇拜中……

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
9 [报告]
发表于 2005-05-16 15:40 |只看该作者

请教:substring在string中的重复次数

[quote]原帖由 "怒剑狂啸"]寂寞烈火真是牛人啊,怎么能记得住那么多命令和参数,并将它们组合运用的啊?极度崇拜中……[/quote 发表:

  我现在特崇拜会perl的兄弟  

论坛徽章:
0
10 [报告]
发表于 2005-05-16 16:33 |只看该作者

请教:substring在string中的重复次数

原帖由 "寂寞烈火" 发表:

  我现在特崇拜会perl的兄弟  

怀疑寂寞烈火是不是linuxsir以前的javalee?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP