in reply to Can we do without auxiliary variable here?

print map { "$prefix$_" } split /\n/, join '', @_
print map { "$prefix$_" } join('',@_) =~ m/([^\n]+\n?)/g

Replies are listed 'Best First'.
Re^2: Can we do without auxiliary variable here?
by ikegami (Patriarch) on Feb 28, 2011 at 17:04 UTC

    print map { "$prefix$_" } split /\n/, join '', @_

    Fixed:

    print map { "$prefix$_" } split /^/m, join('', @_), -1;

    Note: split implies the "m" for that pattern, but it's clearer to specify it.

    Update: As Eliya kinda points out, there won't ever be null trailing fields with that pattern, so the above can be simplified to

    print map { "$prefix$_" } split /^/m, join '', @_;
      Great solution! I completely forgot about the "negativ LIMIT" parameter of split.

      -- 
      Ronald Fischer <ynnor@mm.st>
        I completely forgot about the "negativ LIMIT" parameter

        I think the crucial idea here is to split on ^, not the negative limit. The latter isn't even needed, because there are no null fields in this case.

Re^2: Can we do without auxiliary variable here?
by rovf (Priest) on Feb 28, 2011 at 17:04 UTC
    Compact solution, but unfortunately doesn't fulfil the specification:

    The first solution is not correct, because it would drop the newline characters. The second solution keeps most newline characters, but if the string contains a sequence of \n, all but the first are ignored...

    -- 
    Ronald Fischer <ynnor@mm.st>