in reply to Re: Replacing single quotes to two single quotes inside map
in thread Replacing single quotes to two single quotes inside map
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: <%-{-{-{-<
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Replacing single quotes to two single quotes inside map
by hippo (Archbishop) on Nov 11, 2017 at 11:48 UTC | |
|
Re^3: Replacing single quotes to two single quotes inside map
by Anonymous Monk on Nov 14, 2017 at 15:09 UTC | |
by choroba (Cardinal) on Nov 14, 2017 at 17:01 UTC | |
by Anonymous Monk on Nov 14, 2017 at 19:47 UTC |