Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Stack overflow while performing nested substitutions

by sivaramanm (Acolyte)
on May 25, 2005 at 05:28 UTC ( [id://460244]=perlquestion: print w/replies, xml ) Need Help??

sivaramanm has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

When compile my perl program, I got an error as 'Memory Stack overflow'. Pls help me to correct this problem.

Please find my codings

My perl code:-- $file=~s#<tbody>((?:[^<]+|<(?!/?tbody>))+</tbody>(?:\s*<tbody>(?:[^<]+ +|<(?!/?tbody>))+</tbody>)*)#"<tbody>\n".${TableBody(\$1)}."\n</tbody> +"#gies; sub TableBody { my ($line) = @_; $temp = "$$line"; $temp =~s#</tbody>$##; $temp =~s#\t#</para></entry><entry><para>#g; $temp ="<row><entry><para>$temp</para></entry></row>"; $temp =~s#<tbody>#<row><entry><para>#g; $temp =~s#</tbody>#</para></entry></row>#g; return \$temp; } My input is:-- <tbody>one two three</tbody> <tbody>one two three</tbody> <tbody>one two three</tbody> <tbody>one two three</tbody> My output is:-- <tbody> <row><entry><para>one</para></entry><entry><para>two</para></entry><en +try><para>three</para></entry></row> <row><entry><para>one</para></entry><entry><para>two</para></entry><en +try><para>three</para></entry></row> <row><entry><para>one</para></entry><entry><para>two</para></entry><en +try><para>three</para></entry></row> <row><entry><para>one</para></entry><entry><para>two</para></entry><en +try><para>three</para></entry></row> </tbody>

It works well. But if the number of rows in the input exceeds 25, the "Memory stack overflow" error comes.

Pls help me, how to increase stack memory.

thanks

Replies are listed 'Best First'.
Re: stack problem
by jdporter (Paladin) on May 25, 2005 at 10:45 UTC
    And lastly, using regexes — or, indeed, any form of direct textual munging — is considered very bad style, when dealing with XML (or XHTML). It looks to me like you're trying to perform a simple transformation which would be easily within the power of XSLT... but if you really want to code it from scratch, at least let one of the fine XML parsers do the hard part for you.
Re: stack problem
by gopalr (Priest) on May 25, 2005 at 05:56 UTC

    Hello

    Without seeing coding how can we solve the problem?

      Quite. Plus, what do you mean by "compile" exactly?


      ($_='kkvvttuu bbooppuuiiffss qqffssmm iibbddllffss')
      =~y~b-v~a-z~s; print
Re: stack problem
by reasonablekeith (Deacon) on May 25, 2005 at 10:14 UTC
    Firstly, running the following works fine on my machine (on a recent active state perl install), and produces output as you describe.
    my $xml = "<tbody>one\ttwo\tthree</tbody>\n"x40; $xml =~ s#<tbody>((?:[^<]+|<(?!/?tbody>))+</tbody>(?:\s*<tbody>(?:[^<] ++|<(?!/?tbody>))+</tbody>)*)#"<tbody>\n".${TableBody(\$1)}."\n</tbody +>"#gies; print $xml; sub TableBody { my ($line) = @_; $temp = "$$line"; $temp =~s#</tbody>$##; $temp =~s#\t#</para></entry><entry><para>#g; $temp ="<row><entry><para>$temp</para></entry></row>"; $temp =~s#<tbody>#<row><entry><para>#g; $temp =~s#</tbody>#</para></entry></row>#g; return \$temp; }
    Secondly, If you're going to give an example, it's nice to give a working example so that people can easily reproduce your error.

    Thirdly, your code seems a bit obfusacted. Without knowing what you're trying to do it's hard to give advice, but I'd suggest a starting point as something like the following, which produces the same results as your script.

    my $xml = "<tbody>one\ttwo\tthree</tbody>\n"x5; $xml =~ s#<tbody>(.*?)</tbody>#${TableBody(\$1)}#gies; print $xml; sub TableBody { my $line_ref = shift; my $return_xml; foreach my $thing (split(/\t/, $$line_ref)) { $return_xml .= "<entry><para>$thing</para></entry>"; } return \"<row>$return_xml</row>"; }
    ---
    my name's not Keith, and I'm not reasonable.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://460244]
Approved by BrowserUk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-09-08 15:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.