Hello, I would like to count the frequency of certain keywords in the text file, which is sample.txt. For example, I determine a main word as "Steve Jobs" and "Executive," and I would like to count the frequency of "stock option" and "package" within 10 words from "Steve Jobs" and "Executive" for the sample text below. The result that I expected is 4.

Sample text) Stock option is the most popular compensation policy in the world these days. Steve Jobs also received huge amount of stock options, and the stock option was exercised before the fiscal year. Different from his compensation package, the other executives received less amount of stock options.

To get the result, I used the code below and used the command that "perl code.pl sample.txt "Steve Jobs" "Executive" 10 "stock option" "package"

However, the error message occurs. The error message is "Use of uninitialized value $distance in numeric le <<=> at line..."

Could you please give me some advice to get the result I want? I am attaching the sample text and the code that I used. In the sample text, there are three different articles and it is divided by "Document ". So, I expect to get the results for the three articles. I am looking forward to your responses. I hope you all have a great weekend! I really appreciate it in advance.

We can see from the dump that $filename contains the filename, but it appears that @mainword has slurped the rest of the arguments, the behaviour I would expect. Therefore we can see now that $distance is undefined from the beginning ( the reason for the "Use of uninitialized value $distance in numeric le <<=> at line..." error received later on ).

The reason for this behaviour is because Perl must be greedy when assigning an array to an array, unless you can provide more information. In your case @mainword is variable length, and Perl doesn't know how many elements it should assign from @ARGV before moving onto $distance.

Here is an adjustment, mainword and search are now comma separated strings and not individual arguments. Once we have read the arguments in, we can split the mainword and search strings up into individual elements:

I followed the first way and the error message is gone now. But, I still cannot get the right results. The results are all "0" Could you please see my revised code? I am attaching my revised code and sample text again. I really appreciate it in advance. I apologize you if it is a really simple question. Because I am a real beginner, I even do not know whether my problems are simple or not. Thank you very much.

The only problem with crossposts is that responders do not want to spend time solving problems that are already solved on another forum. The solution is for you to add a comment to your post listing all crossposts. Good Luck, Bill