Considering you use the word " split" in the title of your post, it's funny you aren't using split to process the text.
our $all_text = join "", <ARGV>; # files, STDIN, etc.
our $key_phrase = "This is "; # should not be hard-coded
our $base_name = "UserA_";
our $ext = ".txt";
our @bits = split m/\Q$key_phrase\E/, $all_text;
# if line 1 data includes the key phrase, element 1 will be empty:
shift @bits if $all_text =~ m/^\Q$key_phrase\E/;
my $count = 1;
foreach my $bit (@bits) {
# suggest padding the index number so files sort correctly
my $filename = sprintf "%s%2.2d%s", $base_name, $count++, $ext;
open FILE, ">", $filename
or die "Could not write to \"$filename\": $!\n";
print FILE "$key_phrase$bit"; # put back the what split() excised
close FILE;
}
This solution assumes that you can read all the data into memory, of course, but unless it's a million lines or an ongoing TCP/IP connection or something, I rarely have issues with that.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|