Re: Replacing a line



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"}

}

}

.



Relevant Pages

  • Re: Packages and returning errors
    ... > array intact. ... sub is_a_instance_method { ... my $class = shift; ... You need to fix the scope of $error by moving its declaration outside ...
    (comp.lang.perl.misc)
  • Re: passing database data to a sub
    ... > I'm not sure of the difference, why isn't it a subroutine? ... > sure about this 'shift' thing anyway :-) ... > sub teardown ... > # Setup the template to use for the output. ...
    (perl.beginners)
  • Re: Packages and returning errors
    ... The perldoc function guide says about shift (which is ... "Shifts the first value of the array off and returns it, ... If an array of values are passed to a sub, ... Back to my package (which I am currently thinking might be out of my depth, ...
    (comp.lang.perl.misc)
  • Re: any pointers please? combine words script
    ... ugly but i hate repeated code like that sub has. ... sf> my $source = shift; ... never name a temp, temp. ... sf> return @array; ...
    (comp.lang.perl.misc)
  • Passing an array as Object
    ... I'm trying to write a subroutine that will fill an array of some type with several objects of that type. ... Private MyObjectsAs SomeObjectType ... Public Sub somemethod() ...
    (microsoft.public.dotnet.languages.vb)