in reply to Splitting and maintaining the delimiter

If the delimiter is a constant then simply doing a join should work to reverse it.

my $delim = ':TAG:'; my @data = split $delim, $line; my $line = join $delim, @data;

If the delimiter varies, ie. is a regexp, then things get more fun, but I'll only think about that if needed.

The other obvious solution is to keep a copy of the original line about, no point rejoining if it's practical to simply throw it back out.

Update: Misunderstood the question somewhat. If you're happy with keeping the tag at the start then a lookahead regexp assertion may be just what you're looking for. Have a look at this.
my $data = ":TAG:This:TAG:is:TAG:a:TAG:test:TAG:"; my @results = split /(?=:TAG:)/, $data;

I've not played with these things properly so I have no idea how performant they may be, however.

Replies are listed 'Best First'.
Re: Re: Splitting and maintaining the delimiter
by Rich36 (Chaplain) on May 23, 2002 at 16:00 UTC

    It's not so much that I need to join the lines back together... What I've got is a sizable tagged data file. What I need to do is split them up by the tags, then get the character count of the data, including the tags. I was just curious if there was a better or more efficient way to do it. I thought about trying to do something with seek, but I believe that would involve taking two passes at the data to do so. I'll probably just go with something like:

    my $delim = ':TAG:'; my @data = split $delim, $line; @data = map{ $_ = $delim . $_ } @data; print length($_) . "\n" foreach @data;

    Thanks,
    Rich36

    UPDATE: See Molt's update on the previous post. Works great...