Replacing a line
am 06.08.2007 16:35:04 von vedpsingh
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;
}
}
}
Re: Replacing a line
am 06.08.2007 17:17:32 von glex_no-spam
Ved 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 ?
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
Re: Replacing a line
am 06.08.2007 17:36:03 von vedpsingh
> > for (@array) {
> > if (/\//#define $dirFileLoadInput 1/) #what to do here ??
> > {
> > $_ = #define $dirFileLoadinput 1 ;
> > last;
>
> You only want to modify the first one?
>
> > }
No, I want to modify all
Thanks for the reply.
Re: Replacing a line
am 06.08.2007 18:52:48 von glex_no-spam
Ved wrote:
>>> for (@array) {
>>> if (/\//#define $dirFileLoadInput 1/) #what to do here ??
>>> {
>>> $_ = #define $dirFileLoadinput 1 ;
>>> last;
>> You only want to modify the first one?
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`
Re: Replacing a line
am 07.08.2007 00:31:58 von Tad McClellan
Ved wrote:
> //#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!;
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
Re: Replacing a line
am 07.08.2007 11:32:16 von vedpsingh
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;
}
}
}
Re: Replacing a line
am 07.08.2007 13:08:21 von Tad McClellan
Ved wrote:
> 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.
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
Re: Replacing a line
am 08.08.2007 18:40:13 von vedpsingh
On Aug 7, 3:17 am, "J. Gleixner"
wrote:
> Ved 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 ?
>
> 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
Hi Gleixner,
Why is it not entering in this condition ?
if( m{^//#define \$module_nameFileLoadInput 1$} )
Re: Replacing a line
am 09.08.2007 02:38:16 von Tad McClellan
Ved wrote:
[ 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" }
------------------------------
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
Re: Replacing a line
am 09.08.2007 21:06:40 von glex_no-spam
Ved wrote:
> On Aug 7, 3:17 am, "J. Gleixner"
> wrote:
>> Ved 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
[...]
>> 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
Re: Replacing a line
am 11.08.2007 15:16:52 von vedpsingh
On Aug 9, 5:38 am, Tad McClellan wrote:
> Ved wrote:
>
> [ 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" }
> ------------------------------
>
> --
> Tad McClellan
> email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
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
Re: Replacing a line
am 11.08.2007 18:41:33 von Tad McClellan
Ved wrote:
> On Aug 9, 5:38 am, Tad McClellan wrote:
>> Ved wrote:
>>
>> [ 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.
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.
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
Re: Replacing a line
am 16.08.2007 08:26:35 von vedpsingh
On Aug 6, 7:35 pm, Ved 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"}
}
}
Re: Replacing a line
am 16.08.2007 08:31:45 von vedpsingh
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"}
}
}