Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
Think about Loose Coupling
 
PerlMonks  

Testing if a string is a substring

by monkfan (Curate)
on Jul 20, 2005 at 04:43 UTC ( [id://476421]=perlquestion: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.

monkfan has asked for the wisdom of the Perl Monks concerning the following question:

Guys,
I'm wondering if there is a better/faster way to do it. Here is my current working code.
#!/usr/bin/perl -w use strict; my $super = 'AAAAATT'; my $sub = 'AT'; # Returns 1 (True) #my $sub = 'AAA'; # Returns 1 (True) #my $sub = 'GGG'; # Returns 0 (False) print is_subst($super,$sub), "\n"; sub is_subst { my ($super,$sub) = @_; my $sup_len = length $super; my $sub_len = length $sub; foreach (my $i = 0; $i<$sup_len; $i++) { my $str = substr($super,$i,$sub_len); if ( $str eq $sub ) { return 1; last; } } return 0; }
Regards,
Edward

Replies are listed 'Best First'.
Re: Testing if a string is a substring
by monarch (Priest) on Jul 20, 2005 at 04:50 UTC
    #!/usr/bin/perl -w use strict; my $super = 'AAAAATT'; my $sub = 'AT'; # Returns 1 (True) #my $sub = 'AAA'; # Returns 1 (True) #my $sub = 'GGG'; # Returns 0 (False) print "1\n" if ( $super =~ m/\Q$sub\E/ );
      I wouldn't use a regex for this, there aren't any patterns, just plain old string matching
      my $super = 'AAAAATT'; my $sub = 'AT'; print "1\n" if (index($super, $sub) != -1);
      ---
      my name's not Keith, and I'm not reasonable.
Re: Testing if a string is a substring
by davorg (Chancellor) on Jul 20, 2005 at 04:57 UTC

    Ouch. Do you like making life hard for yourself :)

    I think you're looking for the index function.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: Testing if a string is a substring
by gjb (Vicar) on Jul 20, 2005 at 05:01 UTC

    The index functions is also your friend, see the relevant perldoc.

    However, for serious work (i.e. long strings) you may want to have a look at the following book:

    @BOOK{Gusf97,
      title = {Algorithms on strings, trees and sequences},
      publisher = {Cambridge University Press},
      year = {1997},
      author = {Dan Gusfield},
      address = {Cambridge},
    }
    
    or the BioPerl project.

    Hope this helps, -gjb-

Re: Testing if a string is a substring
by Zaxo (Archbishop) on Jul 20, 2005 at 05:01 UTC

    That is what index is made for:

    sub is_subst { my ($string, $subst) = @_; -1 != index( $string, $subst); }
    You may just want to call index directly, since it returns the location in $string of the first match.

    After Compline,
    Zaxo

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://476421]
Approved by prasadbabu
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.