Multiline substitution

Discussion in 'Perl Misc' started by gooliver, Nov 21, 2006.

  1. gooliver

    gooliver Guest

    I've this text on a TeX-file source.tex:

    bla bla bla...
    $$
    some equation
    $$
    bla bla bla...

    and i want convert source.tex in a file dest.tex where $$
    are replaced from the couple \[...\]

    bla bla bla...
    \[
    some equation
    \]
    bla bla bla...

    but i've encountered most difficulties about pattern-matching
    of the multiline string $$...$$ !!!

    Have you some suggestion?

    ---
    thanx in advance!!!
     
    gooliver, Nov 21, 2006
    #1
    1. Advertising

  2. * gooliver <> wrote:
    >
    > I've this text on a TeX-file source.tex:
    >
    > bla bla bla...
    > $$
    > some equation
    > $$
    > bla bla bla...
    >
    > and i want convert source.tex in a file dest.tex where $$
    > are replaced from the couple \[...\]
    >
    > bla bla bla...
    > \[
    > some equation
    > \]
    > bla bla bla...
    >
    > but i've encountered most difficulties about pattern-matching
    > of the multiline string $$...$$ !!!
    >
    > Have you some suggestion?


    Assuming two $ characters never appear next to each other in
    other circumstances:

    $code =~ s{ \$\$ (.*?) \$\$ }{\\[$1\\]}gxms;

    --
    Lars Haugseth

    "If anyone disagrees with anything I say, I am quite prepared not only to
    retract it, but also to deny under oath that I ever said it." -Tom Lehrer
     
    Lars Haugseth, Nov 21, 2006
    #2
    1. Advertising

  3. gooliver

    gooliver Guest

    > Assuming two $ characters never appear next to each other in
    > other circumstances:
    >
    > $code =~ s{ \$\$ (.*?) \$\$ }{\\[$1\\]}gxms;
    >


    ....but i've the lines of my source.tex file one-by-one:

    while ($r = <IN> ) {
    $line = ...
    ...
    print OUT $line; }
     
    gooliver, Nov 21, 2006
    #3
  4. * gooliver <> wrote:
    >
    >> Assuming two $ characters never appear next to each other in
    >> other circumstances:
    >>
    >> $code =~ s{ \$\$ (.*?) \$\$ }{\\[$1\\]}gxms;

    >
    > ...but i've the lines of my source.tex file one-by-one:
    >
    > while ($r = <IN> ) {
    > $line = ...
    > ...
    > print OUT $line; }


    Do you have to do it this way? Consider using slurp:

    my $code = do { local $/; <IN> };

    If you're processing one line at a time, you'll have to use a flag
    indicating whether you're inside a $$-block or not, and substituting
    accordingly.

    --
    Lars Haugseth

    "If anyone disagrees with anything I say, I am quite prepared not only to
    retract it, but also to deny under oath that I ever said it." -Tom Lehrer
     
    Lars Haugseth, Nov 21, 2006
    #4
  5. gooliver

    Paul Lalli Guest

    gooliver wrote:
    > > Assuming two $ characters never appear next to each other in
    > > other circumstances:
    > >
    > > $code =~ s{ \$\$ (.*?) \$\$ }{\\[$1\\]}gxms;
    > >

    >
    > ...but i've the lines of my source.tex file one-by-one:
    >
    > while ($r = <IN> ) {
    > $line = ...
    > ...
    > print OUT $line; }


    "Doctor, Doctor, it hurts when I raise my arm like this!!"

    Paul Lalli
     
    Paul Lalli, Nov 21, 2006
    #5
  6. gooliver

    gooliver Guest

    In data Tue, 21 Nov 2006 14:10:29 +0100, Paul Lalli <> ha
    scritto:

    > "Doctor, Doctor, it hurts when I raise my arm like this!!"


    The doctor replies, "Then don't raise your arm over your head." (-:

    ....
    $code="";
    while ( $r = <IN> ) {
    $code = $code . $r;
    }
    close(IN);
    $code =~ s{ \$\$ (.*?) \$\$ }{\\[$1\\]}gxms;
    print OUT $code;
    close(OUT);
     
    gooliver, Nov 21, 2006
    #6
  7. gooliver

    -berlin.de Guest

    gooliver <> wrote in comp.lang.perl.misc:
    > > Assuming two $ characters never appear next to each other in
    > > other circumstances:
    > >
    > > $code =~ s{ \$\$ (.*?) \$\$ }{\\[$1\\]}gxms;
    > >

    >
    > ...but i've the lines of my source.tex file one-by-one:
    >
    > while ($r = <IN> ) {
    > $line = ...
    > ...
    > print OUT $line; }


    Then your subject is misleading. You don't have multiline
    substitutions.

    my @replace = qw( [ ]);
    my $which = 0;
    while ( <DATA> ) {
    s/^\$\$$/$replace[ $which]/ and $which = 1 - $which;
    print;
    }

    Anno
     
    -berlin.de, Nov 21, 2006
    #7
  8. gooliver

    Uri Guttman Guest

    >>>>> "g" == gooliver <> writes:

    g> In data Tue, 21 Nov 2006 14:10:29 +0100, Paul Lalli <>
    g> ha scritto:

    >> "Doctor, Doctor, it hurts when I raise my arm like this!!"


    g> The doctor replies, "Then don't raise your arm over your head." (-:

    g> ...
    g> $code="";

    not needed with .=.

    and you didn't use strict or warnings.

    g> while ( $r = <IN> ) {
    g> $code = $code . $r;
    g> }

    gack! use File::Slurp. yours is the slowest way to read in whole files.

    g> close(IN);
    g> $code =~ s{ \$\$ (.*?) \$\$ }{\\[$1\\]}gxms;
    g> print OUT $code;

    File::Slurp can also handle outputting files.

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, Nov 21, 2006
    #8
    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. Justin

    adobe multiline substitution

    Justin, Dec 8, 2003, in forum: Perl
    Replies:
    0
    Views:
    501
    Justin
    Dec 8, 2003
  2. cw
    Replies:
    1
    Views:
    625
    James J. Foster
    Jun 27, 2003
  3. Michael Tanner

    Different fonts in multiline text box...

    Michael Tanner, Jul 25, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    609
    Steve C. Orr, MCSD
    Jul 25, 2003
  4. Justin

    adobe multiline substitution

    Justin, Dec 8, 2003, in forum: Perl Misc
    Replies:
    2
    Views:
    112
    Jim Keenan
    Dec 9, 2003
  5. dale zhang
    Replies:
    8
    Views:
    426
    Tintin
    Nov 30, 2004
Loading...

Share This Page