468,321 Members | 1,669 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Substring doubt?

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
4 1352
nithinpes
410 Expert 256MB
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
4,059 Expert 2GB
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
410 Expert 256MB
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
4,059 Expert 2GB
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.

Similar topics

7 posts views Thread by cpp_weenie | last post: by
7 posts views Thread by Radhika Sambamurti | last post: by
7 posts views Thread by Chris Lasher | last post: by
4 posts views Thread by Jean-François Michaud | last post: by
11 posts views Thread by dyc | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by howard w | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.