Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   Writing results to file??? (http://www.velocityreviews.com/forums/t896253-writing-results-to-file.html)

jchludzinski@gmail.com 01-24-2006 11:07 AM

Writing results to file???
 
I used the following code to change '//...' C++ comments to standard
'/*...*/' C-commenting (I'm sure there's a better way of doing this?).

while ( defined ($_ = <FILE>) )
{
if ($_ =~ s|\/\/|\/\*|)
{
$_ =~ s|\n|\*\/\n|;
print ("Line: ", $_);
}
}

Now how can I write the results back out to file? (Right now the only
thing I've changed is $_.)

---John


Gunnar Hjalmarsson 01-24-2006 11:15 AM

Re: Writing results to file???
 
jchludzinski@gmail.com wrote:
> Now how can I write the results back out to file?


Open the file for writing and print to it.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

calle.erlandsson@gmail.com 01-24-2006 11:45 AM

Re: Writing results to file???
 
Why dont you do

print map{"Line: ".$_}grep{s!//(.*)!/*$1*/!;1}<>

and pipe it in the right direction...


Jürgen Exner 01-24-2006 12:23 PM

Re: Writing results to file???
 
jchludzinski@gmail.com wrote:
> Now how can I write the results back out to file? (Right now the only
> thing I've changed is $_.)


See "perldoc -q append":
How do I change one line in a file/delete a line in a file/insert a line
in the middle of a file/append to the beginning of a file?

jue



Paul Lalli 01-24-2006 01:57 PM

Re: Writing results to file???
 
jchludzinski@gmail.com wrote:
> I used the following code to change '//...' C++ comments to standard
> '/*...*/' C-commenting (I'm sure there's a better way of doing this?).
>
> while ( defined ($_ = <FILE>) )
> {
> if ($_ =~ s|\/\/|\/\*|)
> {
> $_ =~ s|\n|\*\/\n|;
> print ("Line: ", $_);
> }
> }
>
> Now how can I write the results back out to file? (Right now the only
> thing I've changed is $_.)


You may be interested in looking into perl's -i option, which will
automatically print your modified lines back to the original file.
Using that option, your code above becomes a one-liner:

perl -pi -e's{\n}{\*/\n} if s{//}{/*}' file.c

perldoc perlrun
for more information on -p and -i (including how to make a backup copy
while you're changing the file)

Now, whether or not your RegExp logic is the best way to tackle this is
another matter entirely.

I would suggest instead just one operation:
s{ // (.*) } { /* $1 */ }x;

Paul Lalli


DJ Stunks 01-24-2006 04:10 PM

Re: Writing results to file???
 
jchludzinski@gmail.com wrote:
> I used the following code to change '//...' C++ comments to standard
> '/*...*/' C-commenting (I'm sure there's a better way of doing this?).
>
> while ( defined ($_ = <FILE>) )
> {
> if ($_ =~ s|\/\/|\/\*|)
> {
> $_ =~ s|\n|\*\/\n|;
> print ("Line: ", $_);
> }
> }
>
> Now how can I write the results back out to file? (Right now the only
> thing I've changed is $_.)
>
> ---John


Instead of rolling your own regexp, why not take advantage of the
modules:

perl -MRegexp::Common -pi~ -e \
's{ $RE{comment}{C++}{-keep} }{ /* $3 */ }xms' file.c

no sense reinventing the wheel.

-jp


calle.erlandsson@gmail.com 01-25-2006 07:27 AM

Re: Writing results to file???
 
w00t!

Nice!


Anno Siegel 02-13-2006 01:03 PM

Re: Writing results to file???
 
Paul Lalli <mritty@gmail.com> wrote in comp.lang.perl.misc:
> jchludzinski@gmail.com wrote:
> > I used the following code to change '//...' C++ comments to standard
> > '/*...*/' C-commenting (I'm sure there's a better way of doing this?).


> I would suggest instead just one operation:
> s{ // (.*) } { /* $1 */ }x;


That introduces some spurious blanks. The effect of /x doesn't extend
to the right side of a substitution :)

s{ // (.*) } {/*$1 */}x;

looks about right.

Anno
--
$_='Just another Perl hacker'; print +( join( '', map { eval $_; $@ }
'use warnings FATAL => "all"; printf "%-1s", "\n"', 'use strict; a',
'use warnings FATAL => "all"; "@x"', '1->m') =~
m|${ s/(.)/($1).*/g; \ $_ }|is),',';

Paul Lalli 02-13-2006 01:06 PM

Re: Writing results to file???
 
Anno Siegel wrote:
> Paul Lalli <mritty@gmail.com> wrote in comp.lang.perl.misc:
> > jchludzinski@gmail.com wrote:
> > > I used the following code to change '//...' C++ comments to standard
> > > '/*...*/' C-commenting (I'm sure there's a better way of doing this?).

>
> > I would suggest instead just one operation:
> > s{ // (.*) } { /* $1 */ }x;

>
> That introduces some spurious blanks. The effect of /x doesn't extend
> to the right side of a substitution :)
>
> s{ // (.*) } {/*$1 */}x;
>
> looks about right.


Hrm. Quite correct.... but did you really need to go back almost a
whole month to find an error I'd made? I'm sure I've made many more
errors more recently than that! :)

Thanks for the correction,
Paul Lalli


Anno Siegel 02-13-2006 01:17 PM

Re: Writing results to file???
 
Paul Lalli <mritty@gmail.com> wrote in comp.lang.perl.misc:
> Anno Siegel wrote:
> > Paul Lalli <mritty@gmail.com> wrote in comp.lang.perl.misc:
> > > jchludzinski@gmail.com wrote:
> > > > I used the following code to change '//...' C++ comments to standard
> > > > '/*...*/' C-commenting (I'm sure there's a better way of doing this?).

> >
> > > I would suggest instead just one operation:
> > > s{ // (.*) } { /* $1 */ }x;

> >
> > That introduces some spurious blanks. The effect of /x doesn't extend
> > to the right side of a substitution :)
> >
> > s{ // (.*) } {/*$1 */}x;
> >
> > looks about right.

>
> Hrm. Quite correct.... but did you really need to go back almost a
> whole month to find an error I'd made? I'm sure I've made many more
> errors more recently than that! :)


Ugh. I hadn't noticed it was so old and wouldn't have commented if
I had.

Anno
--
$_='Just another Perl hacker'; print +( join( '', map { eval $_; $@ }
'use warnings FATAL => "all"; printf "%-1s", "\n"', 'use strict; a',
'use warnings FATAL => "all"; "@x"', '1->m') =~
m|${ s/(.)/($1).*/g; \ $_ }|is),',';


All times are GMT. The time now is 08:16 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.