write a perl script to find whether a given line of text is
starting and ending with same word or not ???
Answers were Sorted based on User's Feedback
Answer / guest
Lets assume that the text to match is present in a file
say "data.txt".
Following program will print the line containing same
starting and ending word.
open(FILE,"data.txt") or die "cannot open file : $!";
while(<FILE>) {
if($_ =~ /^(\w+)\s+.*?\1$/) {
print "the line is $_ \n";
}
}
| Is This Answer Correct ? | 5 Yes | 1 No |
Answer / vipul dalwala
Above solution ($_ =~ /^(\w+)\s+.*?\1$/) does not work for
string like "vipul on allinterview.com onvipul";
I would suggest :
if( $_ =~ /^([^\s]+)(\s.*?)*\s\1$/ ) {
print "the line is $_ \n";
}
| Is This Answer Correct ? | 3 Yes | 3 No |
How many types of variable in perl?
How do I send e-mail from a Perl/CGI program on a Unix system?
What is the purpose of “_file_ literal” and “_line_ literal” in perl?
What are arrays in perl?
When does circular reference occur?
Explain the difference between die and exit in perl?
What does undef function in perl?
Can you add two arrays together?
How do you you check the return code of a command in perl?
Why is it hard to call this function: sub y { "because" } ?
Explain goto label?
Explain goto label, goto name, and goto expr?