Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Replacing a line

Reply
Thread Tools

Replacing a line

 
 
Ved
Guest
Posts: n/a
 
      08-06-2007
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;
}
}
}

 
Reply With Quote
 
 
 
 
J. Gleixner
Guest
Posts: n/a
 
      08-06-2007
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
 
Reply With Quote
 
 
 
 
Ved
Guest
Posts: n/a
 
      08-06-2007
> > 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.

 
Reply With Quote
 
J. Gleixner
Guest
Posts: n/a
 
      08-06-2007
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`
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      08-06-2007
Ved <(E-Mail Removed)> 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/"
 
Reply With Quote
 
Ved
Guest
Posts: n/a
 
      08-07-2007
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;
}
}
}


 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      08-07-2007
Ved <(E-Mail Removed)> 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/"
 
Reply With Quote
 
Ved
Guest
Posts: n/a
 
      08-08-2007
On Aug 7, 3:17 am, "J. Gleixner" <(E-Mail Removed)>
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$} )

 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      08-09-2007
Ved <(E-Mail Removed)> 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/"
 
Reply With Quote
 
J. Gleixner
Guest
Posts: n/a
 
      08-09-2007
Ved wrote:
> On Aug 7, 3:17 am, "J. Gleixner" <(E-Mail Removed)>
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Read a file line by line and write each line to a file based on the5th byte scad C++ 23 05-17-2009 06:11 PM
How to read a text file line by line and remove some line kaushikshome C++ 4 09-10-2006 10:12 PM
Beginner: read $array with line breaks line by line Marek Stepanek Perl Misc 12 09-02-2006 10:27 AM
Replacing - and not Replacing... Rob Meade ASP General 5 04-11-2005 06:49 PM
Read a file line by line with a maximum number of characters per line Hugo Java 10 10-18-2004 11:42 AM



Advertisments