2

Ok, so I'm trying to make my own markup language [for a class, I know it's dumb] and I'm having a lot of trouble getting it run. It seems to get stuck (just pauses and I assume is caught in a loop.) as soon as I add the text that causes the arraySplitter function to be called. I can provide information and output if you'd like. This seems like a simple error, as I am rather new to perl.

#JokeCode -> HTML

use strict;
use warnings;
use Getopt::Std;
my %opts=();

theFunctionThatActuallyParsesTheNonsense($opts{i});
my $questionCounter;
my $tempVar;
my @arrayForSplit;
my @goodArray;
my $curLine;
sub theFunctionThatActuallyParsesTheNonsense
{
my $outputF = $_[0];
$questionCounter= 0;

while (<LINES>)
{
      $curLine= $_;
        if($curLine =~ m/&&url/)
    {

        @arrayForSplit = split(/ /,$_);
        $curLine =~ s/$arrayForSplit[1]//;
        $curLine =~ s/$arrayForSplit[2]//;
        $curLine =~ s/&&url/<a href="$arrayForSplit[1]">$arrayForSplit[2]/;
        $curLine =~ s/url&&/<\/a>/;
        print @arrayForSplit;

    }


    $curLine =~ s/\&\&b /<b>/;
    $curLine =~ s/ b\&\&/<\/b>/;

    $curLine =~ s/&&i /<i>/;
    $curLine =~ s/ i&&/<\/i>/;

    $curLine =~ s/&&u /<u>/;
    $curLine =~ s/ u&&/<\/u>/;

    $curLine =~ s/&&tt/<tt>/;
    $curLine =~ s/tt&&/<\/tt>/;

    $curLine =~ s/&&grn/<span style="color:#00ff00">/;
    $curLine =~ s/grn&&/<\/span>/;

    $curLine =~ s/&&red/<span style="color:#ff0000">/;
    $curLine =~ s/red&&/<\/span>/;

    $curLine =~ s/&&blu/<span style="color:#0000ff">/;
    $curLine =~ s/blu&&/<\/span>/;

    $curLine =~ s/&&ex/<pre class="code">/;
    $curLine =~ s/ex&&/<\/pre>/;

    if($curLine =~  m/&&tf/)
    {
        $curLine =~ s/&&tf/<form><br><input type="text" name="$questionCounter"><\/form>/;
        $curLine =~ s/tf&&//;
        $questionCounter++;
    }
    print "even here?";
    if($curLine =~ m/&&mc/)
    {
        print "How bout now?";
        @goodArray = arraySplitter($curLine);

        foreach (@goodArray)
        {

            $curLine =~ s/&&mc/<input type="radio" name="$questionCounter" value="$_"> $_ <br>/;
        }
        $curLine =~ s/&&mc/<br>/;
        $curLine =~ s/mc&&/<input type="radio" name="questionCounter" style="display: none;"\/>/;
        $questionCounter++;
    }   
    print OUT $curLine;
    print OUT "\n";

}


sub arraySplitter
{
print "Are you still there?";
my @retArray;
my $arrayPlace = 0;
my @mcArray = split(/ /,$_[0]);
print @mcArray;
    if(scalar(@mcArray)-1 < 1)
    {
    print "The size of the array is 0";
    exit 1;
    }
while ($arrayPlace < scalar(@mcArray)-1)
{
    $retArray[$arrayPlace] = $mcArray[$arrayPlace];
}
print @retArray;
return @retArray;
}
}
Phil Colson
  • 141
  • 3
  • 9
  • Although it is known that [you should not parse HTML/XML with regexes](http://stackoverflow.com/a/1732454/459233), Tom Christiansen implemented a parser in his response: [Oh Yes You Can Use Regexes to Parse HTML!](http://stackoverflow.com/a/4234491/459233) I'd recommend you to use some parsers like [XML::LibXML](http://search.cpan.org/~shlomif/XML-LibXML-2.0010/LibXML.pod), [XML::Simple](http://search.cpan.org/~grantm/XML-Simple-2.20/lib/XML/Simple.pm), or even [Marpa::HTML](http://search.cpan.org/~jkegl/Marpa-HTML-0.112000/lib/Marpa/HTML/Doc/HTML.pod) if you don't have strict requirements reg – Tudor Constantin Nov 05 '12 at 15:31

1 Answers1

2

This section:

while ($arrayPlace < scalar(@mcArray)-1)
{
    $retArray[$arrayPlace] = $mcArray[$arrayPlace];
}

…never modifies $arrayPlace. So you have an infinite loop.


It looks like it is trying to make a copy of @mcArray, which would be better done with:

 @retArray = @mcArray;

… but since it looks like you simply return @retArray, you might as well just:

return @mcArray;

If this is unfinished code and you intend to do some sort of transformation, then look into map.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335