Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^2: Replacing single quotes to two single quotes inside map

by AnomalousMonk (Chancellor)
on Nov 10, 2017 at 23:33 UTC ( #1203151=note: print w/replies, xml ) Need Help??


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:

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:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^3: Replacing single quotes to two single quotes inside map
by hippo (Abbot) on Nov 11, 2017 at 11:48 UTC

    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.

Re^3: Replacing single quotes to two single quotes inside map
by Anonymous Monk on Nov 14, 2017 at 15:09 UTC
    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.

      Update:

      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,
        You mean that this way:
        map { (my $dx = $data->{ $_ } || '') =~ s//'/g; $dx } @allparms

        Will not replace 0s with empty string, correct?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2018-04-25 04:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?