469,360 Members | 1,774 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,360 developers. It's quick & easy.

How to match repeat characters in Perl?

1
Hello,

I'm new to Perl and I'm stepping through an online tutorial. I'm trying to count the lines in a file that have strings with double letters in them. This is my code -

Expand|Select|Wrap|Line Numbers
  1. $file = 'c:\Perl\Beth\f7.txt';
  2. open(INFO, $file);
  3. $i = 1;
  4. while ($line = <INFO>)
  5. {
  6.     $_ = $line;
  7.     if (/.+/)
  8.     {  
  9.       print "00$i $line";
  10.       $i++;
  11.     }
  12.    }
  13. close(INFO);
"f7.txt" contains:

orannges
lemon lemon
pickles hammburger
bagel

This compiles and I get:

001 orannges
002 lemon lemon
003 pickles hammburger
004 bagel

I thought the ".+" would only match strings with one or more repeating characters. I expected:

001 orannges

What did I do wrong?

Thanks!
Beth
Nov 29 '07 #1
10 4506
Hello,

I'm new to Perl and I'm stepping through an online tutorial. I'm trying to count the lines in a file that have strings with double letters in them. This is my code -

Expand|Select|Wrap|Line Numbers
  1. $file = 'c:\Perl\Beth\f7.txt';
  2. open(INFO, $file);
  3. $i = 1;
  4. while ($line = <INFO>)
  5. {
  6.     $_ = $line;
  7.     if (/.+/)
  8.     {  
  9.       print "00$i $line";
  10.       $i++;
  11.     }
  12.    }
  13. close(INFO);
"f7.txt" contains:

orannges
lemon lemon
pickles hammburger
bagel

This compiles and I get:

001 orannges
002 lemon lemon
003 pickles hammburger
004 bagel

I thought the ".+" would only match strings with one or more repeating characters. I expected:

001 orannges

What did I do wrong?

Thanks!
Beth

If my guess is not wrong, the output you want is :

001 orannges
003 pickles hammburger

Is it right?
Dec 3 '07 #2
KevinADC
4,059 Expert 2GB
.+ would match one or more of any character in the lines. It will not match repeated characters that are the same. For that you would use:

/(.)\1/

or if you only want to count repeated word characters

/(\w)\1/

post a link to the online tutorial you are using.
Dec 3 '07 #3
jonathan184
154 100+
Thats right ........................................
Dec 3 '07 #4
Plz try this...

Expand|Select|Wrap|Line Numbers
  1. $file = 'c:\Perl\Beth\f7.txt';
  2. open(INFO, $file);
  3. while ($line = <INFO>)
  4. {
  5.     while($line =~m{(\w)}igs)
  6.     {
  7.       $a = $1;
  8.       if($line =~m{$a{2}}is)
  9.       {
  10.           print "$a-$line\n";
  11.       }
  12.    }
  13. }
  14. close(INFO);
  15.  
Dec 4 '07 #5
Sorry. try this....
Expand|Select|Wrap|Line Numbers
  1. $file = 'c:\Perl\Beth\f7.txt';
  2. open(INFO, $file);
  3. while ($line = <INFO>)
  4. {
  5.     while($line =~m{(\w)}igs)
  6.     {
  7.       $a = $1;
  8.       if($line =~m{$a{2}}is)
  9.       {
  10.           print "$a-$line\n";
  11.           last;
  12.       }
  13.    }
  14. }
  15. close(INFO);
  16.  
Dec 4 '07 #6
numberwhun
3,503 Expert Mod 2GB
Sorry. try this....
Expand|Select|Wrap|Line Numbers
  1. $file = 'c:\Perl\Beth\f7.txt';
  2. open(INFO, $file);
  3. while ($line = <INFO>)
  4. {
  5.     while($line =~m{(\w)}igs)
  6.     {
  7.       $a = $1;
  8.       if($line =~m{$a{2}}is)
  9.       {
  10.           print "$a-$line\n";
  11.           last;
  12.       }
  13.    }
  14. }
  15. close(INFO);
  16.  
I understand that you are trying to be helpful, but that is two posts in a row without the proper code tags.

Please be sure and use them next time!

Regards,

Jeff
Dec 4 '07 #7
You can try this one too..

Expand|Select|Wrap|Line Numbers
  1. $file = 'c:\Perl\Beth\f7.txt'
  2. open(INFO, $file);
  3. while ($_ = <INFO>)
  4. {
  5.     $line = $_;
  6.     $line =~ s/(\d+)(\w+)/$2/g;
  7.     if($line =~ /(.)\1/)
  8.     {
  9.       print $line;
  10.     }
  11.    }
  12. close(INFO);
Dec 4 '07 #8
archulu
34
hello friend
plz write ur problem properly, with neat example.(not ur code)
Dec 5 '07 #9
numberwhun
3,503 Expert Mod 2GB
hello friend
plz write ur problem properly, with neat example.(not ur code)
First, please refrain from the SMS speak. You can expand plz to 'please' and 'ur' to your. We are not limited to the 160 character SMS limit here in the forums.

Second, why should the user NOT post their code? We go through painstaking measures and repeated posts requesting the users code because we need to see it. We also like to have a sample of any data they are parsing. Any information that the user can provide (errors, code, data, etc) is always helpful, especially when we are trying to troubleshoot an issue. Please do not go telling an OP not to post their code, as we always ask them to.

Regards,

Jeff
Dec 5 '07 #10
RedSon
5,000 Expert 4TB
First, please refrain from the SMS speak. You can expand plz to 'please' and 'ur' to your. We are not limited to the 160 character SMS limit here in the forums.

Second, why should the user NOT post their code? We go through painstaking measures and repeated posts requesting the users code because we need to see it. We also like to have a sample of any data they are parsing. Any information that the user can provide (errors, code, data, etc) is always helpful, especially when we are trying to troubleshoot an issue. Please do not go telling an OP not to post their code, as we always ask them to.

Regards,

Jeff
I think it is important to note here that we ask OPs to post their relevant code not their entire code. But if their entire code consists of less then 20 lines then posting the whole code is not an issue.

It is inappropriate for users to go around willy-nilly and tell other users to not post their entire code but just examples, that is the moderators job. You don't need to do that.

Further, I take a very hard line on words like "plz" and "ur". You get only one warning from me, and then if I see it again, the user who does it will be met with severe consequences.

-MODERATOR
Dec 5 '07 #11

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

20 posts views Thread by Ravi | last post: by
9 posts views Thread by Ron Adam | last post: by
3 posts views Thread by bdwise | last post: by
6 posts views Thread by Matt Wette | last post: by
2 posts views Thread by Sriram | last post: by
3 posts views Thread by learner | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.