Replacing a line

V

Ved

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;
}
}
}
 
J

J. Gleixner

Ved said:
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 ?

perldoc -f rename
2) What to do in this to get (/ //#define $dirFileLoadInput 1 /) in
the for loop ?
#!/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;

Why have $dir and $file? Use more descriptive variable
names.

open( my $cpp_files, '<', $dir_list )
or die "Cannot read $dir_list: $!\n";
while ( my $cpp_file = <$cpp_files> )
{
chomp $cpp_file;
if ( -e $cpp_file )
{
process_one( $cpp_file );
}
else { ...}
}
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;

Since you're processing a 'file' not a 'dir'ectory, name your
variables accordingly:

my $file = shift;

print "Processing $file\n";
etc.
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;

You only want to modify the first one?

for( @array )
{
# Need to escape the $
if( m{^//#define \$dirFileLoadInput 1$} )
{
s{^//}{};
last;
}
}

Documentation on regular expressions:

perldoc perlretut
 
J

J. Gleixner

I should have said "the first occurrence of that line in the file."
No, I want to modify all

If you want to modify all occurrences, then remove the 'last;'

perldoc -f last

You could shorten it a lot, using a few command line options (untested):

perl -pi -e 's{^//#define \$dirFileLoadInput 1$}{#define
\$dirFileLoadInput 1}g' `cat file_containing_cpp_files`

Or a bit shorter as:

perl -pi -e 's{^//}{} if m{^//#define \$dirFileLoadInput 1$}' `cat
file_containing_cpp_files`
 
T

Tad McClellan

Ved said:
//#define (FileName)FileLoadInput 1
is to be replaced with
#define (FileName)FileLoadInput 1
1) Remove trailing .cpp in $dir ?


$dir =~ s/\.cpp$//;

2) What to do in this to get (/ //#define $dirFileLoadInput 1 /) in
the for loop ?

if (/\//#define $dirFileLoadInput 1/) #what to do here ??


s!//(#define ${dir}FileLoadInput 1)!$1!;
 
V

Ved

Thanks for replies,

Below is the code I have modified now.
$module_name and $cpp_file are coming perfectly fine.

But, I am not able to see this:
print "Finally I am here \n";

It means that "if" condition is not being fuifilled.
if( m{^//#define \$module_nameFileLoadInput $} )
^
|
Is something missing at (this place)
i.e. after $module_name in the if condtion ?


Regards
Ved




#####################################################

#!/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>) {
chop $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";
chomp $module_name;#added
if (-e $cpp_file) {
process_one_file($cpp_file);
} 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 = shift;
print "Processing $cpp_file\n";
tie my @array, 'Tie::File', $cpp_file or die "tie error
$cpp_file: $!\n" ;

for (@array)
{
print "I am at this place \n";
# Need to escape the $
if( m{^//#define \$module_nameFileLoadInput $} )
#if( s!//(#define ${module_name}FileLoadInput 1)!$1!)
{
print "Finally I am here \n";
s{^//}{};
# last;
}
}
}
 
T

Tad McClellan

Ved said:
It means that "if" condition is not being fuifilled.
if( m{^//#define \$module_nameFileLoadInput $} )
^
|
Is something missing at (this place)


Yes, a closing curly bracket.

i.e. after $module_name in the if condtion ?


You also need an opening curly bracket in the appropriate place.

You also do NOT need (or want) the dollar sign to be backslashed.

while (my $cpp_file = <$fh>) {
chop $cpp_file;


You should use chomp() instead of chop() for removing newlines.

my $module_name = "$cpp_file";


my $module_name = $cpp_file;

See:

perldoc -q vars

What’s wrong with always quoting "$vars"?


$module_name =~ s/\.cpp$//;#removing trainling .cpp
print "this is name $module_name\n";
print "this is CPP $cpp_file\n";
chomp $module_name;#added


Why did you think that adding that was a good idea?

What did you expect that it would do for you?

(hint: it does nothing for you here.)

if( m{^//#define \$module_nameFileLoadInput $} )
#if( s!//(#define ${module_name}FileLoadInput 1)!$1!)


I did not mean that you should put the s/// in the if condition.

I meant that you should use the s/// _instead of_ the if condition.

There is no need to check if it matches first, simply do the s///
and it will do nothing if it does not happen to match.
 
V

Ved

perldoc -f rename





Why have $dir and $file? Use more descriptive variable
names.

open( my $cpp_files, '<', $dir_list )
or die "Cannot read $dir_list: $!\n";
while ( my $cpp_file = <$cpp_files> )
{
chomp $cpp_file;
if ( -e $cpp_file )
{
process_one( $cpp_file );
}
else { ...}

}


Since you're processing a 'file' not a 'dir'ectory, name your
variables accordingly:

my $file = shift;

print "Processing $file\n";
etc.


You only want to modify the first one?


for( @array )
{
# Need to escape the $
if( m{^//#define \$dirFileLoadInput 1$} )
{
s{^//}{};
last;
}
}

Documentation on regular expressions:

perldoc perlretut
Hi Gleixner,

Why is it not entering in this condition ?

if( m{^//#define \$module_nameFileLoadInput 1$} )
 
T

Tad McClellan

[ Please learn to compose followups properly.
Quoting 100 lines to add 2 lines will make you go invisible...
]

Why is it not entering in this condition ?

if( m{^//#define \$module_nameFileLoadInput 1$} )


Probably because the string in $_ does not contain a '$' followed
by an 'm' followed by an 'o' followed by a 'd'...




You should attempt to write a short and complete program that we
can run that illustrates your problem.

Have you seen the Posting Guidelines that are posted here frequently?


------------------------------
#!/usr/bin/perl
use warnings;
use strict;

$_ = '//#define BBPRxChanRouteFileLoadInput 1';
my $module_name = 'BBPRxChanRoute';
if( m{^//#define ${module_name}FileLoadInput 1$} )
{ print "true\n" }
else
{ print "false\n" }
 
J

J. Gleixner

Ved said:
Ved said:
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
[...]
for( @array )
{
# Need to escape the $
if( m{^//#define \$dirFileLoadInput 1$} )
{
s{^//}{};
last;
}
}

Documentation on regular expressions:

perldoc perlretut
Hi Gleixner,

Why is it not entering in this condition ?

if( m{^//#define \$module_nameFileLoadInput 1$} )

Sorry, my fault. I misssed that $dir was a variable, in
your match. For some reason I thought you were trying to
actually match a string that contained '$dirFileLoadInput'.

${module_name}FileLoadInput
 
V

Ved

[ Please learn to compose followups properly.
Quoting 100 lines to add 2 lines will make you go invisible...
]
Why is it not entering in this condition ?
if( m{^//#define \$module_nameFileLoadInput 1$} )

Probably because the string in $_ does not contain a '$' followed
by an 'm' followed by an 'o' followed by a 'd'...

You should attempt to write a short and complete program that we
can run that illustrates your problem.

Have you seen the Posting Guidelines that are posted here frequently?

------------------------------
#!/usr/bin/perl
use warnings;
use strict;

$_ = '//#define BBPRxChanRouteFileLoadInput 1';
my $module_name = 'BBPRxChanRoute';
if( m{^//#define ${module_name}FileLoadInput 1$} )
{ print "true\n" }
else
{ print "false\n" }


Hi Tad and Gleixner,
Thanks for the replies.
Tad I will be more adherant to the guidelines in future.

Now the code looks like this:
################################################################
#!/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>) {
chop $cpp_file;
our $module_name = "$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);
} 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 = 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( m{^//#define ${module_name}FileLoadInput 1$} )
{ print "true $module_name \n"
s{^//}{}; # Is something wrong here ?
}
else
#{ print "false\n" }
{ print "false $module_name \n"}

}


}

############## my.rxfiles contains this ###############
abc.cpp


############### abc.cpp contains this ###########
//#define abcFileLoadInput 1
// #define abcFileLoadInput 1

##################################################
##################################################
PROBLEM (1)
Now when I use "strict" I am getting following error:

Variable "$module_name" is not imported at rx_top_script.pl line 30.
Variable "$module_name" is not imported at rx_top_script.pl line 31.
Variable "$module_name" is not imported at rx_top_script.pl line 36.
Global symbol "$module_name" requires explicit package name at
rx_top_script.pl line 30.
Global symbol "$module_name" requires explicit package name at
rx_top_script.pl line 31.
Global symbol "$module_name" requires explicit package name at
rx_top_script.pl line 36.
###################
When I comment strict I am getting this which perfectly fine:
this is name abc
this is CPP abc.cpp
Processing abc.cpp
true abc
false abc
###################
PROBLEM(2)
I thought to ignore STRICT for the time being and try to achieve what
I desire.
So in the for loop I put the line :
s{^//}{};
##################
But it gave me this error:
syntax error at rx_top_script.pl line 32, near "s{^//}{}"
##################

Do I need to rectify the STRICT issue(problem) to get rid of
PROBLEM(2) ?

If yes :
Is this the best link to solve PROBLEM(1) ?
http://www.perl.com/pub/a/2002/04/23/mod_perl.html

Or is there something even better?

Regards
Ved
 
T

Tad McClellan

Ved said:
[ Please learn to compose followups properly.
Quoting 100 lines to add 2 lines will make you go invisible...
]


[ Please learn to compose followups properly.
Please do this very soon.
]



You should attempt to write a short and complete program that we
can run that illustrates your problem.

Tad I will be more adherant to the guidelines in future.


Now is the future!

#use strict;


You lose all of the benfits of the strict pragma when you comment it out.

chop $cpp_file;


You should still use chomp() instead of chop() for removing newlines.

our $module_name = "$cpp_file";
^^^
^^^

Why did you put that there?

Why are you still quoting that lone variable?

Are you reading the followups?

Is there anybody in there? Just nod if you can hear me.

if( m{^//#define ${module_name}FileLoadInput 1$} )
{ print "true $module_name \n"
s{^//}{}; # Is something wrong here ?
}
else
{ print "false $module_name \n"}



As I also told you before, there is no need to test the match
before doing the substitution, so you should not test the
match before doing the substitution.

if ( s{^//(#define ${module_name}FileLoadInput 1)}{$1} ) {
print "true $module_name\n"
}
else {
print "false $module_name\n"
}


PROBLEM (1)
Now when I use "strict" I am getting following error:
Global symbol "$module_name" requires explicit package name at
rx_top_script.pl line 30.


"Coping with Scoping":

http://perl.plover.com/FAQs/Namespaces.html

and

perldoc strict

and

perldoc my


PROBLEM(2)
I thought to ignore STRICT for the time being and try to achieve what
I desire.


That is not a good thought. Abandon it.

Instead learn how to control the scope of variables in Perl.

Do I need to rectify the STRICT issue(problem)


Yes. Unless you _like_ spending time finding bugs that a program
could have found for you.

to get rid of
PROBLEM(2) ?


but not for that reason.

Rather for the reason that using strict will automatically find
bugs in your programs for you, so you don't have to find them yourself.

If yes :
Is this the best link to solve PROBLEM(1) ?
http://www.perl.com/pub/a/2002/04/23/mod_perl.html


No, since that is about mod_perl, and you are not using mod_perl (or are you?).

Or is there something even better?


You should always prefer lexical (my) variables over package (our)
variables, except when you can't.

I see nothing in what you are trying to do that indicates that
you can't, so you should be using lexical (my) variables.
 
V

Ved

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

}

}
 
V

Ved

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

}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

PHP failed to create file 13
Command Line Arguments 0
Sorting 3
readdir conflict problem? 3
Merge files 1
read and parse a single line file 21
purge line 13
Help with Hash 2

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top