Re: Can we do without auxiliary variable here?
by Corion (Patriarch) on Feb 28, 2011 at 16:23 UTC
|
I'm not sure that extracting things is the right approach. Instead I'd try to replace all newlines with \n$prefix (resp. all "starts" of the string with the prefix, in REspeak):
sub prefix_print {
# unfortunately, the local copy of $_ is necessary to be able to p
+refix_print
# constants
print map { my $l = $_; $l =~ s/^/$prefix/gm; $l } @_
};
prefix_print qq(foo\nbar),2,3,4
| [reply] [d/l] [select] |
Re: Can we do without auxiliary variable here?
by ig (Vicar) on Feb 28, 2011 at 17:58 UTC
|
prefix_print("A\nB");
prefix_print("C\n");
Update: I might do something like the following:
| [reply] [d/l] [select] |
|
|
Good catch. I imagine a "\n" would need to be added.
| [reply] |
Re: Can we do without auxiliary variable here?
by Eliya (Vicar) on Feb 28, 2011 at 16:59 UTC
|
sub prefix_print {
print map $_ ne "\n" ? "$prefix$_":$_, split /(\n)/, join '',@_;
}
The split /(\n)/ also returns the newlines, so you can prefix anything that isn't a newline, while passing the newlines through as is. This way, you don't have to take care of boundary cases like trailing newline and initial prefix. | [reply] [d/l] [select] |
Re: Can we do without auxiliary variable here?
by ikegami (Patriarch) on Feb 28, 2011 at 20:32 UTC
|
my $s = join('', @_); $s =~ s/^/$prefix/mg; print $s;
print map { $prefix . $_ } split /^/m, join('', @_);
print apply { s/^/$prefix/mg } join('', @_);
print join('', @_) =~ s/^/$prefix/rmg;
apply comes from List::MoreUtils
The last requires Perl 5.14 for /r.
| [reply] [d/l] [select] |
Re: Can we do without auxiliary variable here?
by locked_user sundialsvc4 (Abbot) on Feb 28, 2011 at 20:52 UTC
|
Being an old crotchety person by now, I take the Clark Gable Approach™ to such things: “Frankly, my dear, I don’t give a damn”™ about how you write it, as long as what you write is abundantly clear. The original version of this code was, indeed, clear. (Therefore: priceless.) Save the rest for “Obfuscation” and “Poetry.”
Q: Can we do without an auxiliary variable here?
A: Clark Gable.™
| |
|
|
| [reply] |
|
|
| [reply] [d/l] [select] |
Re: Can we do without auxiliary variable here?
by Anonymous Monk on Feb 28, 2011 at 16:18 UTC
|
print map { "$prefix$_" } split /\n/, join '', @_
print map { "$prefix$_" } join('',@_) =~ m/([^\n]+\n?)/g
| [reply] [d/l] [select] |
|
|
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 '', @_;
| [reply] [d/l] [select] |
|
|
| [reply] [d/l] |
|
|
|
|
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>
| [reply] [d/l] |
Re: Can we do without auxiliary variable here?
by roboticus (Chancellor) on Feb 28, 2011 at 18:10 UTC
|
#!/usr/bin/perl
use warnings;
use strict;
my $prefix="prefix: ";
sub printpre {
unshift @_, $prefix;
print @_;
}
printpre "apple ", "banana ", "tomfoolery ", "\n";
...roboticus
When your only tool is a hammer, all problems look like your thumb. | [reply] [d/l] |
|
|
| [reply] |
|
|
#!/usr/bin/perl
use strict;
use warnings;
my $prefix="Prefix: ";
sub print_prefix {
print $prefix, @_;
}
print_prefix "alpha ", "beta ", "gamma\n";
Sigh. When I read your reply, I thought, "Hmm, does unshift return a reference to the array?", so a first I thought you were prompting me to do something like:
print unshift @_, $prefix;
But when I checked the docs, I could see that it wouldn't fly. Then I understood what you meant. Ah, well! I'm always amused at how my mind works: it seems I frequently complicate simple things. ;^)
...roboticus
When your only tool is a hammer, all problems look like your thumb. | [reply] [d/l] [select] |
|
|
It seems to me the OP has a logger and he wants to include the prefix at the start of every line, not the start of every message. Those aren't the same if there's a possibility of multi-line messages.
| [reply] |
|
|
| [reply] [d/l] |
|
|
sub print_prefix {
local $/="\nPrefix: ";
print @_;
}
But that didn't work. On first guess, I thought it would do OK. After reading the documentation, I thought it would print it at least once. But what I got was...nothing. It never printed prefix at all. The only other methods I could think of had already been covered.
...roboticus
When your only tool is a hammer, all problems look like your thumb. | [reply] [d/l] |
Re: Can we do without auxiliary variable here?
by ig (Vicar) on Feb 28, 2011 at 19:47 UTC
|
Could someone explain where these matches come from?
Calling your second version with prefix_print('y'), I get "PyP", not "PyPP". I am testing with perl 5.12.2, default compile from source on linux.
The match returns two matches on my system:
- the 'y' at the end of the line
- then '' (.* can match nothing) at the end of the line
It would be an infinite loop (forever matching nothing at the end of the line) except for Repeated Patterns Matching a Zero length Substring
I can't imagine how you got "PyPP"
| [reply] |