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

push @array, qq{*$_*\n};
See push.

Replies are listed 'Best First'.
Re^4: question on multi line pattern matching for html formatting
by tallCoolOne (Initiate) on May 19, 2009 at 18:02 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.

        OK, confession time. Still a newbie. Hashes scare me. I am still struggling with getting my head around the basic concept of hashes, and have sat through several bad explanations of what they are(and have the cranial stretch marks to prove it). But your example code above helps a fair amount. Still and all, I can't say that I exactly understand what is taking place, except from your output, it's what I am after. Which is fantastic of course. But secondly, I am running this script on about 430+ files. So listing them as you have in the braces would be a bit problematic. I have the list of text files in an array @textfile, which I get from:

         @textFile = `cat textfiles*.txt`;

        I do some line by line processing on the first few lines of each file, and then the rest of the file winds up in the array @ThisFileArray. At this point, it's just paragraphs of text, which I am trying to wrap with the paragraph formats. If I can use my array of text file names @textfile inside the braces, it would appear that would be a better way to get the list of file names into the code structure. Then I need to stare long and hard at your code to better understand what takes place there.
        It would seem that I could change your

        my @files = qw{file1.txt file2.txt};

        To my @textfile;, and go from there. I just have to understand what going from there entails, and where I would be going.
        I really appreciate your help and support with all this, and it will go a long way to help me realize my efforts to put together a clean, cool looking website.
        Thanks very much for your time and efforts to help me out.
        Mark