Writing results to file???

Discussion in 'Perl Misc' started by jchludzinski@gmail.com, Jan 24, 2006.

  1. Guest

    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
     
    , Jan 24, 2006
    #1
    1. Advertising

  2. Gunnar Hjalmarsson, Jan 24, 2006
    #2
    1. Advertising

  3. Guest

    Why dont you do

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

    and pipe it in the right direction...
     
    , Jan 24, 2006
    #3
  4. 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
     
    Jürgen Exner, Jan 24, 2006
    #4
  5. Paul Lalli Guest

    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
     
    Paul Lalli, Jan 24, 2006
    #5
  6. DJ Stunks Guest

    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
     
    DJ Stunks, Jan 24, 2006
    #6
  7. Guest

    w00t!

    Nice!
     
    , Jan 25, 2006
    #7
  8. Anno Siegel Guest

    Paul Lalli <> wrote in comp.lang.perl.misc:
    > 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),',';
     
    Anno Siegel, Feb 13, 2006
    #8
  9. Paul Lalli Guest

    Anno Siegel wrote:
    > Paul Lalli <> wrote in comp.lang.perl.misc:
    > > 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
     
    Paul Lalli, Feb 13, 2006
    #9
  10. Anno Siegel Guest

    Paul Lalli <> wrote in comp.lang.perl.misc:
    > Anno Siegel wrote:
    > > Paul Lalli <> wrote in comp.lang.perl.misc:
    > > > 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),',';
     
    Anno Siegel, Feb 13, 2006
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Analog Guy

    Writing Testbench Output Results

    Analog Guy, Sep 29, 2004, in forum: VHDL
    Replies:
    1
    Views:
    1,327
    Jim Lewis
    Sep 29, 2004
  2. HNguyen
    Replies:
    4
    Views:
    2,437
    HNguyen
    Dec 21, 2004
  3. Monique Y. Mudama
    Replies:
    1
    Views:
    472
    Monique Y. Mudama
    Jun 28, 2005
  4. lovecreatesbeauty
    Replies:
    8
    Views:
    1,723
    Old Wolf
    Sep 12, 2005
  5. Ken Fine
    Replies:
    3
    Views:
    561
    Steven Cheng [MSFT]
    Jul 23, 2008
Loading...

Share This Page