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 |
What are the different types of perl operators?
What are scalars?
How to prevent file truncation in perl?
Why do we use "use strict" in perl?
Perl uses single or double quotes to surround a zero or more characters. Are the single(' ') or double quotes (" ") identical?
What is perl programming?
How many types of primary data structures in Perl and what do they mean?
Which has the highest precedence, List or Terms? Explain?
what is the difference between require and use in perl?
What are the different string manipulation operators in perl?
In Perl, there are some arguments that are used frequently. What are that arguments and what do they mean?
How would you ensure the re-use and maximum readability of your perl code?