Re: Replacing a line
- From: Ved <vedpsingh@xxxxxxxxx>
- Date: Wed, 15 Aug 2007 23:26:35 -0700
On Aug 6, 7:35 pm, Ved <vedpsi...@xxxxxxxxx> wrote:
Hi all,
I have to replace commented line in a list of " .cpp" file .
Line looks like this:
//#define BBPRxChanRouteFileLoadInput 1
i.e. The line format :
//#define (FileName)FileLoadInput 1
is to be replaced with
#define (FileName)FileLoadInput 1
I have written a code using Tie::File which is mentioned below.
I want to do two things:
1) Remove trailing .cpp in $dir ?
2) What to do in this to get (/ //#define $dirFileLoadInput 1 /) in
the for loop ?
For above example file name in my.rxfiles is:
BBPRxChanRoute.cpp
Thanks in advance
Ved
#######################################################
#!/usr/local/bin/perl
use strict;
use warnings;
use Tie::File;
my $dir_list = 'my.rxfiles'; #contains list of .cpp files
open my $fh,'<',$dir_list or die "Cannot read $dir_list: $!\n";
while (my $dir = <$fh>) {
chop $dir;
my $file = "$dir";
chomp $file;
if (-e $dir) {
process_one_file($dir);
} else {
warn "File $dir does not exist; skipping\n";
}
}
#Using core module Tie::File to process a file in this subroutine
sub process_one_file {
my $dir = shift;
print "Processing $dir\n";
tie my @array, 'Tie::File', $dir or die "tie error $dir: $!
\n" ;
for (@array) {
if (/\//#define $dirFileLoadInput 1/) #what to do here ??
{
$_ = #define $dirFileLoadinput 1 ;
last;
}
}
}
##################################################
Hi All,
Thanks for the replies.
I am posting the code for the completeness of the thread.
My task is being accomplished with this code, though I am still not
sure if it is perfect code.
#!/usr/local/bin/perl
use strict;
use warnings;
use Tie::File;
my $cpp_file_list = 'my.rxfiles';
open my $fh,'<',$cpp_file_list or die "Cannot read $cpp_file_list:
$!\n";
while (my $cpp_file = <$fh>) {
chomp $cpp_file;
my $module_name = "$cpp_file";
$module_name =~ s/\.cpp$//;#removing trainling .cpp
print "this is name $module_name\n";
print "this is CPP $cpp_file\n";
if (-e $cpp_file) {
process_one_file($cpp_file,$module_name);
} else {
warn "File $cpp_file does not exist; skipping\n";
}
}
#Using core module Tie::File to process a file in this subroutine
sub process_one_file {
my($cpp_file,$module_name)=@_;
$cpp_file = shift;
print "Processing $cpp_file\n";
tie my @array, 'Tie::File', $cpp_file or die "tie error
$cpp_file: $!\n" ;
for (@array) #Each line should come one by one
{
#if ( s{^//(#define ${module_name}FileLoadInput 1)}{$1} ) {
if ( s{//(#define ${module_name}FileLoadInput 1)}{$1} ) {
print "true $module_name \n"
# print " $array \n"
}
else
{ print "false $module_name \n"}
}
}
.
- References:
- Replacing a line
- From: Ved
- Replacing a line
- Prev by Date: Re: unable to use perl on windows
- Next by Date: Re: Replacing a line
- Previous by thread: Re: Replacing a line
- Next by thread: FAQ 7.22 How do I create a switch or case statement?
- Index(es):
Relevant Pages
|