Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Multiline substitution

Reply
Thread Tools

Multiline substitution

 
 
gooliver
Guest
Posts: n/a
 
      11-21-2006
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!!!
 
Reply With Quote
 
 
 
 
Lars Haugseth
Guest
Posts: n/a
 
      11-21-2006

* gooliver <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
gooliver
Guest
Posts: n/a
 
      11-21-2006
> 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; }
 
Reply With Quote
 
Lars Haugseth
Guest
Posts: n/a
 
      11-21-2006

* gooliver <(E-Mail Removed)> 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
 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      11-21-2006
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

 
Reply With Quote
 
gooliver
Guest
Posts: n/a
 
      11-21-2006
In data Tue, 21 Nov 2006 14:10:29 +0100, Paul Lalli <(E-Mail Removed)> 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);
 
Reply With Quote
 
anno4000@radom.zrz.tu-berlin.de
Guest
Posts: n/a
 
      11-21-2006
gooliver <(E-Mail Removed)> 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
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      11-21-2006
>>>>> "g" == gooliver <(E-Mail Removed)> writes:

g> In data Tue, 21 Nov 2006 14:10:29 +0100, Paul Lalli <(E-Mail Removed)>
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.velocityreviews.com/forums/(E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
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
how to define a variable to hold a multiline text input in perl from html multiline textbox dale zhang Perl Misc 8 11-30-2004 06:53 AM
adobe multiline substitution Justin Perl Misc 2 12-09-2003 12:17 AM
adobe multiline substitution Justin Perl 0 12-08-2003 08:28 PM
Different fonts in multiline text box... Michael Tanner ASP .Net 1 07-25-2003 05:55 PM
how to limit user input into the multiline text box???? cw ASP .Net 1 06-27-2003 11:57 AM



Advertisments