How do you search a file in Perl?

tim85a

New member
I'm trying to get Perl to search a file for a string held in $data and if its not found print it into the file. The code I've got at the moment is:

open FILE, 'list.txt';
while ( my $data = <DATA> ) {
$data =~ s/(.)(+)/$1.length($1.$2)/ge;
print $data;
}

close FILE;

but it prints $data still so there is two in the file :( I don't mind if it rights over $data with $data or dies, whichever makes a smaller and simpler program. It has to be a match for all of $data, not just part of it.

Thanks for your help.
 
Your program does not match your description. It's not searching for "a string", it's searching for two or more consecutive occurrences of the same character (and replacing that with the character followed by the number of times it appears.
Also you open and close FILE, but you do not read from or write to it.
 
Back
Top