in reply to Re: question on multi line pattern matching for html formatting
in thread question on multi line pattern matching for html formatting

OK this is fantastic - perfect even. It works just as you describe. But to incorporate it into the rest of my script activities, I would like to capture the output of the print statement in an array if possible, and for some reason nothing that I try seems to work. If I use your print, it outputs to the display, with my html paragraph marks just great. But why can't I do something like:

@newArray = qq{*$_*\n};

??
I've played with every variation of this that I can think of, but I guess I am still too inexperienced to see the obvious. Can you help me put this output into a new array so that I can output it later, when I have all the other elements of my text file processed the way I want them?
Thank you so much for a neat, clean solution.
Mark

Replies are listed 'Best First'.
Re^3: question on multi line pattern matching for html formatting
by wfsp (Abbot) on May 19, 2009 at 06:40 UTC
      Yes, that is one of the ways I attempted to capture the text. It turns out that @array never gets "cleared out", and accumulates all of the text from all of the files, printing ever larger blocks of text as it goes through the whole list of text files. I have tried pushing the contents of @array into another array at different points in the loops, and then clearing @array out, but I don't seem to be doing it right. Here is my whole chunk of code - array names have been changed from our examples.

      { local $/ = q{}; for (@ThisFileArray) { chomp; push @NewTextArray, qq{<p>$_</p>\n}; } } push(@FormattedParagraphs,@NewTextArray); @NewTextArray = "";


      So what do I do to capture all of the paragraphs from one text file which are contained in one pass of @ThisFileArray? I have tried moving the second push into different sections of the loop, and I haven't had any luck. I still wind up accumulating all of the text from all of the text files.
      Thanks for sticking with me this far.
      Mark


      OK, I figured it out. Turns out it wasn't that hard, and I was already close thanks to you wfsp. Here's the new code that works.

      { local $/ = q{}; for (@ThisFileArray) { chomp; push @NewTextArray, qq{<p>$_</p>\n}; } @FormattedParagraphs = @NewTextArray; foreach $newline (@FormattedParagraphs) { $newline =~ s/<p><\/p>//; } @NewTextArray = (); @ThisFileArray = (); }


      Clearing out the original array was what was needed to keep it from accumulating text from subsequent files. Once I put that into the overall loop, everything turned out fine.
      I added the foreach loop to go through the resultant output, and take out the extra paragraph marks that were inserted on blank lines. I could probably do the same thing with an if statement before the paragraph gets pushed into the new array. But this works too.
      Thanks for all the help!
      Mark
        A hash of arrays (HoA) might help (see Perl Data Structures Cookbook).
        #!/usr/bin/perl use strict; use warnings; my @files = qw{file1.txt file2.txt}; my %paragraphs; for my $file (@files){ open my $fh, q{<}, $file or die qq{cant open *$file* to read: $!\n}; local $/ = q{}; while (my $para = <$fh>){ chomp $para; push @{$paragraphs{$file}}, sprintf(q{<p>%s</p>}, $para); } } for my $file (sort keys %paragraphs){ printf qq{\n*** %s ***\n\n}, $file; for my $para (@{$paragraphs{$file}}){ print qq{$para\n\n}; } }
        Two text files.

        file1.txt

        and file2.txt

        output:

        update: pasted the wrong output - fixed.

Re^3: question on multi line pattern matching for html formatting
by tallCoolOne (Initiate) on May 19, 2009 at 01:09 UTC
    OK, new guy has a typing problem. Turns out I WAS capturing the output in an array, I just mis-typed the name of that array later when I wanted to print it to see my results. So:

    @newArray = qq{*$_*\n};

    works. I am now figuring out how to pick up each paragraph that is formatted until I get to the end of the file, and save the completed, formatted output into the new array, for later output to the final html file.
    Sorry to use bandwidth when I didn't need to. *embarrasses self for first time in new community*
      Well, it turns out I need help after all. Either I need a better understanding of the overall function of this little routine, or just a simple explanation of how to save one group of text from the original array, to correspond with the text file being processed. It seems to me that the line I'm saving into the new array kicks out one paragraph at a time. I want to accumulate paragraphs from one text file until done, and then print it out. My limited knowledge of this sort of manipulation has me grasping at straws, and all I come up with is the last paragraph in the file. Would you be willing to help me out a little further with a means to save each text file in it's complete form, and then be able to catch the next file, one at a time. I'd be grateful for whatever you could add. Thanks again, Mark