How do I read text file word by word in perl?
-
I want to check 2 words are in same line or not ? how to copy next word from given word in text file ?
-
Answer:
#!/usr/bin/perl use strict; use warnings; my $filename = $ARGV[0]; my $word1 = "word1"; my $word2 = "word2"; my @words; open( my $fh => $filename) || die "Cannot open $filename: $!"; while(my $line = <$fh>) { @words = split(" ",$line); if (grep (/^$word1$/, @words) && grep (/^$word2$/, @words) ) { print "$word1 and $word2 found in line:$line\n" ; } } close($fh); thanks Tirtha for pointing the mistake in the code, I have updated the code to match the exact words not patterns.
Raghav Yadav at Quora Visit the source
Other answers
Just google for it :)
Akshay Shetye
Related Q & A:
- How can I read my yahoo e-mail via Microsoft outlook 2007?Best solution by Yahoo! Answers
- How can I read my text messages from my computer?Best solution by samsung-messages-backup.com
- How do I solve the problem of downloading word document attached to my email?Best solution by support.google.com
- Where can I read text books online for free?Best solution by bookboon.com
- How do I read an ETD extension file?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.