Good afternoon all,

I have a hash containing thousands of lines of text from hundreds of different files. Every time a certain phrase appears in the hash (in the SSCCE below, "This is"), I want to create a new txt file which prints both the phrase and all subsequent text until we reach the next "This is".

So, for instance, I had hoped that the script below would create six text files (named UserA_1, UserA_2 etc.) where the first file contained the text "This is line 1 from text 1 another line here which should be included in the text file with the above line.", the second file contained the text "This is line 2 from text 1", and so on.

However, although the script below creates the 6 new text files (and names them appropriately), it does not actually print anything into the files.
#!/usr/bin/perl use strict; use warnings; #SSCCE: my %mycorpus = ( text1 => "This is line 1 from text 1 another line here which should be included in the text file with the a +bove line. This is line 2 from text 1 This is line 3 from text 1", text2 => "This is line 1 from text 2 This is line 2 from text 2 another line here which should be included in the text file with the a +bove line. This is line 3 from text 2", ); my $count = 1; foreach my $filename (sort keys %mycorpus) { my $outfile; while ($mycorpus{$filename} =~ /This is/g) { close $outfile if $outfile; open $outfile, '>', "UserA_$count.txt" or die "could not open"; $count++; print {$outfile} $_; } }

I have been working on this script for nearly a week, but I can't spot my mistake(s), and thus I would be very grateful for any help.

EDIT:
I probably should have mentioned in my original post that my code here is based on a more basic script that I use to split and print text NOT stored in a hash. This script (reproduced as an SSCCE below) works successfully and returns the desired output.
my $count = 1; my $outfile; while (<DATA>) { if ( my($regex) = /This is/g) { close $outfile if $outfile; open $outfile, '>', "UserA$1_$count.txt" or die "could not open 'UserA$regex.txt' $!"; $count++; } print {$outfile} $_; } __DATA__ This is line 1 from text 1 another line here which should be included in the text file with the a +bove line. This is line 2 from text 1 This is line 3 from text 1 This is line 1 from text 2 This is line 2 from text 2 another line here which should be included in the text file with the a +bove line. This is line 3 from text 2

In reply to Split and print hash based on regex by Maire

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.