Writing results to file???

J

jchludzinski

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
 
C

calle.erlandsson

Why dont you do

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

and pipe it in the right direction...
 
J

Jürgen Exner

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
 
P

Paul Lalli

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
 
D

DJ Stunks

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
 
A

Anno Siegel

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
 
P

Paul Lalli

Anno said:
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
 
A

Anno Siegel

Paul Lalli said:
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
 

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

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top