my @out = map { s/'/"/g; $_ } @in;
This solution has the behavior discussed by duff here and here of also changing the source array:
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'); my @out = map { s/-/+/g; $_; } @in; print Dumper \@in; print Dumper \@out; " $VAR1 = [ 'Maria+s', 'Thano+s' ]; $VAR1 = [ 'Maria+s', 'Thano+s' ];
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:
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 { (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' ];
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: <%-{-{-{-<
In reply to Re^2: Replacing single quotes to two single quotes inside map
by AnomalousMonk
in thread Replacing single quotes to two single quotes inside map
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |