Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

find text in string

by Anonymous Monk
on Sep 13, 2005 at 03:21 UTC ( #491448=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I was wondering if someone could help me, its probably a really stupid question but Im new to perl so please bare with me


I have a text file and I opened each line into an array value. what I need to do is search each array value and if it contains a string then I do something... example:

if ($foo contains 'bar'){
print "foo contains bar";
}

Comment on find text in string
Re: find text in string
by GrandFather (Cardinal) on Sep 13, 2005 at 03:24 UTC

    $foo is not an array, it is a scalar. You could use index to find 'bar' in $foo, but that doesn't sound like what you want. Perhaps you should post the rest of you code and some sample data so we can see what you are trying to do.


    Perl is Huffman encoded by design.
      I know $foo isnt an array, I was just using that as an example.

      a better example might be:
      for ($num=0; $num<=100; num++){ if (@text[$num] contains 'string'){ $cnt++; } } print "string was found on $cnt lines"

        If you want good answers you need to ask good questions. The better you can specify the problem, the better we are able to answer it. You have now had two replies that sugested that you use the index function. Have you tried it?


        Perl is Huffman encoded by design.
        I would probably re-write it a bit

        untested

        use strict; use warnings; open (IN, 'myfile.txt') or die $!; my @lines = <IN>; my $cnt = 0; for my $line (@lines) { if ($line =~ /somekeyword/) { $cnt++; } } print ("somekeyword was found in $cnt lines\n");

        You could shorten this code a LOT but wanted to make it a little more explicit.

        NOTE: You don't have to necessarily put the whole file in an array. You can use while(<IN> and check that way too.

Re: find text in string
by Zaxo (Archbishop) on Sep 13, 2005 at 03:31 UTC

    You may want the index function. It returns -1 if there is no exact match.

    for (@array) { do {something($_)} if -1 != index( $_, 'bar'); }

    For more complicated matching, you probably want a regex match,

    # . . . if m/foo|bar|baz/;

    After Compline,
    Zaxo

Re: find text in string
by chanakya (Friar) on Sep 13, 2005 at 05:19 UTC
    I suggest using grep to search strings in array, or to find number of elements matching a criteria. The points are demonstrated in the code below:
    use strict; use warnings; use Data::Dumper; my @GrepList = ("yesterday", "today", "tomorrow") ; # look for all elements with "to" in the string my @ElementList = grep(/to/i, @GrepList) ; print Dumper(@ElementList); ## In list context, returns the count of number of elements ## that meet the search criteria my $ElementsFound = grep(/to/i, @GrepList) ; print $ElementsFound;
    Hope this helps
    May the force be with you
Re: find text in string
by Codon (Friar) on Sep 13, 2005 at 16:56 UTC
    Perl provides a rich language for searching for patterns within strings via its Regular Expression engine.
    perldoc perlre
    This will give you way more info than you can absorb quickly. Now, based on your original post, you want to iterate over an array and check each element for a pattern (in this case, a string) and do something each time you find a match.
    for my $line (@input) { if ($line =~ /$pattern/) { # Do something print "$line contains $pattern\n"; } }
    If you just want to get a count of lines that contain $pattern, Perl has a grep function (see perldoc -f grep for more on grep). It returns a list of matches based on some test. When evaluated in scalar context, the length of that list, or the number of matches, is returned.
    my $match_cnt = grep { $_ =~ /$pattern/ } @input;
    With Perl, there is always more that one way to do things, but some are obviously better and more appropriate based on your needs.

    Ivan Heffner
    Sr. Software Engineer, DAS Lead
    WhitePages.com, Inc.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://491448]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2014-09-23 05:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (210 votes), past polls