natxo has asked for the wisdom of the Perl Monks concerning the following question:

seemingly something simple but I do not seem to get right.
my $array_ref = [ "cn=a,dc=domain,dc=tld", "cn=b,dc=domain,dc=tld", "cn=c,dc=domain,dc=tld", "cn=d,dc=domain,dc=tld", "cn=e,dc=domain,dc=tld", "cn=f,dc=domain,dc=tld", "cn=g,dc=domain,dc=tld", "cn=h,dc=domain,dc=tld", "cn=i,dc=domain,dc=tld", "cn=j,dc=domain,dc=tld", "cn=k,dc=domain,dc=tld", "cn=m,dc=domain,dc=tld", "cn=n,dc=domain,dc=tld", "cn=o,dc=domain,dc=tld", "cn=l,dc=domain,dc=tld", ]; my @values = map { s/dc=domain,dc=tld/dc=sub,dc=otherdomain,dc=tld/i; +$_ } @$array_ref; print $_, "\n" for @values;
So I want to substitute the regex "dc=domain,dc=tld" by "dc=sub,dc=otherdomain,dc=tld". I get a list of 1s .... (so 1, 1, 1, ...). Any help greatly appreciated.

Edit: solved, added $_ at the end of the regex.

Replies are listed 'Best First'.
Re: map on array reference
by 1nickt (Canon) on Feb 15, 2016 at 19:35 UTC
    Because you are getting the return value of the substitution, instead of the changed text. Try adding the -r flag, introduced in Perl 5.14:
    my @values = map { s/dc=domain,dc=tld/dc=sub,dc=otherdomain,dc=tld/ir +} @$array_ref;
    perl -E ' @ary =( "abc", "cbc", "dbd" ); say for map { s/b/FOO/r } @ary; ' aFOOc cFOOc dFOOd

    The way forward always starts with a minimal test.
Re: Solved: map on array reference
by kcott (Archbishop) on Feb 16, 2016 at 09:55 UTC

    G'day natxo,

    I see you've marked this as "Solved"; however, I thought I'd point out that, because map aliases $_ to each element of the list, you can do your substitution in-place. E.g.

    $ perl -wE 'my $x = [qw{ab ac ad}]; say "@$x"; map { s/a/z/ } @$x; say + "@$x"' ab ac ad zb zc zd

    — Ken