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



write a perl script to find whether a given line of text is starting and ending with same word or n..

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

write a perl script to find whether a given line of text is starting and ending with same word or n..

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

Post New Answer

More CGI Perl Interview Questions

What is the use of now constructor in perl?

0 Answers  


How do you find the length of an array?

0 Answers  


What rules must be followed by modules in perl.

0 Answers  


How many loop control keywords are there in perl?

0 Answers  


Explain what is STDIN, STDOUT and STDERR?

0 Answers  






Name an instance where you used a CPAN module?

3 Answers   IBM, TCS,


How to read a directory in perl?

0 Answers  


Can any1 tell me 2 write the script using perl script 2 looking at a log file 2 see wheather the test has passed or not.

3 Answers   TCS,


How do I debug a perl program?

0 Answers  


What is eval function in perl?

0 Answers  


How will you get the count of parameters passed to a perl subroutine?

0 Answers  


Can inheritance be used in perl? Explain with the help of an example.

0 Answers  


Categories