K
Kapil Khosla
I am new to PERL and I am trying to write a script which can do the
following changes.
int Main() CHANGES TO int main()
#define Main main CHANGES TO #define main main
int Main(String *str) CHANGES TO int main(String *str)
Essentially I am just converting all the occurences of Main to main in
a file.
I read the chapter on Regular expressions and wrote the following
script to do a search for the pattern with Main in an array with the
same requirement.This works correctly for me.
##################
use strict;
my $i;
my @arrstr = ('Mainframe','#define main Main','int Main(String
^sd)','int Main()','DllMain','int Main(ad)','MainMain');
for( $i=0;$i<@arrstr;$i = $i+1 )
{
print "$arrstr[$i]\n";
if ($arrstr[$i] =~ m{\bMain(\(.*\))?$} )
{
print "String matched\n\n";
}
}
#################
Now, I dont know how to use this in a file and replace the string
matched with the corresponding lower case main.
This is what I could write till now.
open FILEHANDLE,"E:\\file.cpp" or die ("Could not open file for
reading");;
my @myfile = <FILEHANDLE>; # get the file in an arraylist
for $line(@myfile)
{
# search for a pattern, if exists, replace with the correct str ?? #
This is what I dont know how to do
}
close FILEHANDLE;
Can you help?
Thanks a lot.
Kapil
following changes.
int Main() CHANGES TO int main()
#define Main main CHANGES TO #define main main
int Main(String *str) CHANGES TO int main(String *str)
Essentially I am just converting all the occurences of Main to main in
a file.
I read the chapter on Regular expressions and wrote the following
script to do a search for the pattern with Main in an array with the
same requirement.This works correctly for me.
##################
use strict;
my $i;
my @arrstr = ('Mainframe','#define main Main','int Main(String
^sd)','int Main()','DllMain','int Main(ad)','MainMain');
for( $i=0;$i<@arrstr;$i = $i+1 )
{
print "$arrstr[$i]\n";
if ($arrstr[$i] =~ m{\bMain(\(.*\))?$} )
{
print "String matched\n\n";
}
}
#################
Now, I dont know how to use this in a file and replace the string
matched with the corresponding lower case main.
This is what I could write till now.
open FILEHANDLE,"E:\\file.cpp" or die ("Could not open file for
reading");;
my @myfile = <FILEHANDLE>; # get the file in an arraylist
for $line(@myfile)
{
# search for a pattern, if exists, replace with the correct str ?? #
This is what I dont know how to do
}
close FILEHANDLE;
Can you help?
Thanks a lot.
Kapil