my @out = map { s/'/"/g; $_ } @in;
This solution has the behavior discussed by duff here and here of also changing the source array:
c:\@Work\Perl\monks>perl -wMstrict -MData::Dumper -le
"my @in = ('Maria-s', 'Thano-s');
my @out = map { s/-/+/g; $_; } @in;
print Dumper \@in;
print Dumper \@out;
"
$VAR1 = [
'Maria+s',
'Thano+s'
];
$VAR1 = [
'Maria+s',
'Thano+s'
];
If this is acceptable, I would rather just get it over with and change (and henceforth use) the source with a
for-loop (rather than a
map built-in: for clarity):
c:\@Work\Perl\monks>perl -wMstrict -MData::Dumper -le
"my @in = ('Maria-s', 'Thano-s');
s/-/+/g for @in;
print Dumper \@in;
"
$VAR1 = [
'Maria+s',
'Thano+s'
];
Prior to Perl version 5.14, aliased substitution can be avoided by substitution-on-assignment:
c:\@Work\Perl\monks>perl -wMstrict -MData::Dumper -le
"print 'perl version: ', $];
;;
my @in = ('Maria-s', 'Thano-s');
my @out = map { (my $mp = $_) =~ s/-/+/g; $mp; } @in;
print Dumper \@in;
print Dumper \@out;
"
perl version: 5.008009
$VAR1 = [
'Maria-s',
'Thano-s'
];
$VAR1 = [
'Maria+s',
'Thano+s'
];
With version 5.14+, the
/r substitution modifier can be used:
c:\@Work\Perl\monks>perl -wMstrict -MData::Dumper -le
"print 'perl version: ', $];
;;
my @in = ('Maria-s', 'Thano-s');
my @out = map { s/-/+/gr } @in;
print Dumper \@in;
print Dumper \@out;
"
perl version: 5.014004
$VAR1 = [
'Maria-s',
'Thano-s'
];
$VAR1 = [
'Maria+s',
'Thano+s'
];
Give a man a fish: <%-{-{-{-<
-
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.