Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re: Replacing single quotes to two single quotes inside map

by thanos1983 (Vicar)
on Nov 10, 2017 at 21:24 UTC ( #1203141=note: print w/replies, xml ) Need Help??

in reply to Replacing single quotes to two single quotes inside map

Hello Anonymous Monk,

Try something like that:

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my @in = ("Maria's", "Thano's"); my @out = map { s/'/"/g; $_ } @in; print Dumper \@out; __END__ $ perl $VAR1 = [ 'Maria"s', 'Thano"s' ];

Update: Got late in my post due to experimentation. Did not copied fellow Monk duff code. Just to avoid confusion.

Hope this helps, BR.

Seeking for Perl wisdom...on the process of learning...not there...yet!

Replies are listed 'Best First'.
Re^2: Replacing single quotes to two single quotes inside map
by AnomalousMonk (Chancellor) on Nov 10, 2017 at 23:33 UTC
    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:  <%-{-{-{-<

      Building on AnomalousMonk's "Prior to Perl version 5.14" example we can come up with this SSCCE:

      use strict; use warnings; use Test::More tests => 3; my $data = {x => "Maria's"}; my $old = \%$data; my @allparms = sort keys %$data; my @oldparms = @allparms; my @expected = ("Maria''s"); my @result = map { (my $f = $data->{$_} || '') =~ s/'/''/g; $f } @allp +arms; is_deeply \@result, \@expected, "apostrophe's are doubled"; is_deeply $data, $old, '$data is unchanged'; is_deeply \@allparms, \@oldparms, '@allparms is unchanged';

      You can then extend $data and @expected to provide a more comprehensive set of test data to prove that the algorithm matches your needs. All of this assumes (contrary to expectation) that choroba's guess that this is actually an XY Problem is incorrect.

      Hi and I hope you can see this again!
      I got it to work, my Perl version is v5.10.1, but I am getting a warning message as this:

      Useless use of a constant in void context at... that's from the line where the call is.

      I am actually replacing MSWord quote with a regular quote since the DB can't handle it, but if I pass a normal single quote using place holders, it works. This how my code looks like now:
      my @allparms = qw( letters loc type name zip); my $sql = exec_select( "call store_proc(" . join(',', ('?') x @allparm +s ) . ")", map { (my $dx = $data->{ $_ }) =~ s//'/g || '' ; $dx; } @ +allparms );
      How could a prevent this warning and why is it happening ?
      Thanks for looking!
        That's because of the || ''. If the substitution returns false, i.e. there's no smart quote to substitute, you replace this return value that goes nowhere ("void context") by an empty string. You probably meant to add it into the assignment instead?
        map { (my $dx = $data->{ $_ } || '') =~ s//'/g; $dx } @allparms

        But that would replace 0's with empty strings. Use // to replace only undefs.


        map { (my $dx = $data->{ $_ } // '') =~ s//'/g; $dx } @allparms
        ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1203141]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (2)
As of 2018-07-18 22:46 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (397 votes). Check out past polls.