Solved

count of word in all lines of file.

Posted on 2008-06-10
6
180 Views
Last Modified: 2013-12-25
any solution to find number of times the word "xyz" in all lines of the file along with line numbers ?
0
Comment
Question by:Desire2change
6 Comments
 
LVL 39

Accepted Solution

by:
Adam314 earned 500 total points
ID: 21753746

my $count=0;

open(my $IN, "<file.txt") or die "could not open file: $!\n";

while(<$IN>) {

    my $count1=()=/xyz/g;

    $count += $count1;

    print "$.: $count1 times\n" if $count1;

}

close($IN);

Open in new window

0
 
LVL 40

Expert Comment

by:evilrix
ID: 21755665
>> any solution to find number of times the word "xyz"
If you are looking to count the word then you'll need to change the regex suggested by Adam314 from /xyz/ to /\bxyz\b/ to ensure you include word boundries, othrewise you'll count xyz as part of strings rather than as a full word in its own right.

http://www.regular-expressions.info/wordboundaries.html
0
 
LVL 19

Expert Comment

by:Kim Ryan
ID: 21755818
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:Desire2change
ID: 21756642
Adham sorry , i am in favour of the code .but instead i clicked NO , can you pls explain this line -

 my $count1=()=/xyz/g;

0
 
LVL 84

Expert Comment

by:ozo
ID: 21756878
perldoc perlop
       m/PATTERN/msixpogc
       /PATTERN/msixpogc
               Searches a string for a pattern match, and in scalar context
               returns true if it succeeds, false if it fails.  If no string
               is specified via the "=~" or "!~" operator, the $_ string is
               searched.  (The string specified with "=~" need not be an
               lvalue--it may be the result of an expression evaluation, but
               remember the "=~" binds rather tightly.)  See also perlre.  See
               perllocale for discussion of additional considerations that
               apply when "use locale" is in effect.

               Options are as described in "qr//"; in addition, the following
               match process modifiers are available:

                   g   Match globally, i.e., find all occurrences.
                   c   Do not reset search position on a failed match when /g is in effect.

               If "/" is the delimiter then the initial "m" is optional.  With
...
               The "/g" modifier specifies global pattern matching--that is,
               matching as many times as possible within the string.  How it
               behaves depends on the context.  In list context, it returns a
               list of the substrings matched by any capturing parentheses in
               the regular expression.  If there are no parentheses, it
               returns a list of all the matched strings, as if there were
               parentheses around the whole pattern.

perldoc perldata
       List value constructors

       List assignment in scalar context returns the number of elements
       produced by the expression on the right side of the assignment:

           $x = (($foo,$bar) = (3,2,1));       # set $x to 3, not 2
           $x = (($foo,$bar) = f());           # set $x to f()'s return count

       This is handy when you want to do a list assignment in a Boolean
       context, because most list functions return a null list when finished,
       which when assigned produces a 0, which is interpreted as FALSE.

       It's also the source of a useful idiom for executing a function or
       performing an operation in list context and then counting the number of
       return values, by assigning to an empty list and then using that
       assignment in scalar context. For example, this code:

           $count = () = $string =~ /\d+/g;

       will place into $count the number of digit groups found in $string.
       This happens because the pattern match is in list context (since it is
       being assigned to the empty list), and will therefore return a list of
       all matching parts of the string. The list assignment in scalar context
       will translate that into the number of elements (here, the number of
       times the pattern matched) and assign that to $count. Note that simply
       using

           $count = $string =~ /\d+/g;

       would not have worked, since a pattern match in scalar context will
       only return true or false, rather than a count of matches.
0
 

Author Closing Comment

by:Desire2change
ID: 31465861
thanks
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now