Substring doubt?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lilly07
    New Member
    • Jul 2008
    • 89

    #1

    Substring doubt?

    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;
    Code:
    my $string = 'Nowisthetimeforallgoodpeopletocometotheaidoftheirparty';
    Suppose if I want to look for a word
    Code:
    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
    Code:
    Nowisthe
    is the one I am interested. string before time is
    Code:
    Nowisthe
    and the length of the string is 8. How do I do it programatically ? Thanks.
  • nithinpes
    Recognized Expert Contributor
    • Dec 2007
    • 410

    #2
    If you are trying to capture the string preceeding the match, you can make use of the special variable $`. e.g
    Code:
    my $string = 'Nowisthetimeforallgoodpeopletimetocometotheaidoftheir  party'; 
    
    if($string=~/time/) {
    my $pre = $`; # get characters preceeding the match
    my $length=length($pre);
    print "$pre  : ($length)\n"
    }
    This will produce:
    Code:
    Nowisthe  : (8)
    Similarly, $' can be used to capture string succeeding the match.

    Comment

    • KevinADC
      Recognized Expert Specialist
      • Jan 2007
      • 4092

      #3
      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:

      Code:
      my $string = 'Nowisthetimeforallgoodpeopletocometotheaidoftheirparty';
      my $where = index $string, 'time';
      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: @-

      Code:
      my $string = 'Nowisthetimeforallgoodpeopletimetocometotheaidoftheirparty'; 
      if($string=~/time/) {
         print $-[0]; # get the offset
      }

      Comment

      • nithinpes
        Recognized Expert Contributor
        • Dec 2007
        • 410

        #4
        Originally posted by KevinADC
        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: @-

        Code:
        my $string = 'Nowisthetimeforallgoodpeopletimetocometotheaidoftheirparty'; 
        if($string=~/time/) {
           print $-[0]; # get the offset
        }
        That's cool :) . I never knew of @-.

        Comment

        • KevinADC
          Recognized Expert Specialist
          • Jan 2007
          • 4092

          #5
          Theres also @+. You can read about them in perlvar

          Comment

          Working...