By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,834 Members | 2,265 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,834 IT Pros & Developers. It's quick & easy.

Substring doubt?

P: 89
Hi

Please let me know any fnction to do this. I have started learning perl and any guidance would be useful.

Suppose I have a string as below;
Expand|Select|Wrap|Line Numbers
  1. my $string = 'Nowisthetimeforallgoodpeopletocometotheaidoftheirparty';
Suppose if I want to look for a word
Expand|Select|Wrap|Line Numbers
  1. time
, I can use a regex to find whether he string contains the word time or not.

But I need a way to chop the original string and count the number of characters in that string.

In tha above example
Expand|Select|Wrap|Line Numbers
  1. Nowisthe
is the one I am interested. string before time is
Expand|Select|Wrap|Line Numbers
  1. Nowisthe
and the length of the string is 8. How do I do it programatically? Thanks.
Nov 4 '08 #1
Share this Question
Share on Google+
4 Replies


nithinpes
Expert 100+
P: 410
If you are trying to capture the string preceeding the match, you can make use of the special variable $`. e.g
Expand|Select|Wrap|Line Numbers
  1. my $string = 'Nowisthetimeforallgoodpeopletimetocometotheaidoftheir  party'; 
  2.  
  3. if($string=~/time/) {
  4. my $pre = $`; # get characters preceeding the match
  5. my $length=length($pre);
  6. print "$pre  : ($length)\n"
  7. }
  8.  
This will produce:
Expand|Select|Wrap|Line Numbers
  1. Nowisthe  : (8)
  2.  
Similarly, $' can be used to capture string succeeding the match.
Nov 4 '08 #2

KevinADC
Expert 2.5K+
P: 4,059
If you are really wanting to find where a substring is in a string as versus finding a pattern, the index function is made for that:

Expand|Select|Wrap|Line Numbers
  1. my $string = 'Nowisthetimeforallgoodpeopletocometotheaidoftheirparty';
  2. my $where = index $string, 'time';
  3. print $where;
The suggestion nithinpes has posted is an easy one to implement as it takes advantage of some of perls builtin variables and is easy to stick into a script and getting working. It unfortunately inccurs a considerable performance penalty on any other regexps used in the code. An alternative is to use another perl builtin that does not introduce the performance penalty: @-

Expand|Select|Wrap|Line Numbers
  1. my $string = 'Nowisthetimeforallgoodpeopletimetocometotheaidoftheirparty'; 
  2. if($string=~/time/) {
  3.    print $-[0]; # get the offset
  4. }
Nov 4 '08 #3

nithinpes
Expert 100+
P: 410
The suggestion nithinpes has posted is an easy one to implement as it takes advantage of some of perls builtin variables and is easy to stick into a script and getting working. It unfortunately inccurs a considerable performance penalty on any other regexps used in the code. An alternative is to use another perl builtin that does not introduce the performance penalty: @-

Expand|Select|Wrap|Line Numbers
  1. my $string = 'Nowisthetimeforallgoodpeopletimetocometotheaidoftheirparty'; 
  2. if($string=~/time/) {
  3.    print $-[0]; # get the offset
  4. }
That's cool :) . I never knew of @-.
Nov 5 '08 #4

KevinADC
Expert 2.5K+
P: 4,059
Theres also @+. You can read about them in perlvar
Nov 5 '08 #5

Post your reply

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