Hi Natxo, Please see my comment below: On Thu, May 16, 2013 at 8:57 PM, Natxo Asenjo <natxo.ase...@gmail.com>wrote:
> hi, > > in a ldap script where I get a list of values of a multivalued attribute > like this: > > @memberof = qw( cn=group1,cn=xxx,dc=domain,dc=tld > cn=group2,cn=xxx,d=domain,dc=tld etc etc) ; > Since you use "qw", there is no need to still separate the element of your array with comma. > > I would like to use map to convert the list of elements to > @memberof = qw( group1 group2 group3 etc etc ) > Does etc etc here mean words like group...? > > This is the code I have tried to capture the first string after the first > '=' in the $1 variable > > @memberof = map { s/^cn=(.*),.*$/$1/g; $_ } @memberof; > for ( @memberof ) { > print "[$_]" . " " ; > } > > but this snippet strangely cuts the start and the end of every array > element beginning and end so I get this: > > [group1,cn=xxx,dc=domain] [group2,cn=xxxx,dc=domain] > > so it is obviously not working. Is there a way to do this with map or do I > just have to process the array in a for loop and fill a new array with the > values? Just curious > Understanding for/foreach loop could help in a way to understand and use map function. Please see below one of the way of doing what you intended: [CODE] use warnings; use strict; my @memberof = qw( cn=group1 cn=xxx dc=domain dc=tld cn=group2 cn=xxx d=domain dc=tld etc etc); ## using for loop for my $grp (@memberof) { if ( $grp =~ m/(?<=\=)(.+)/ ) { my $matched = $1; print $matched, $/ if $matched =~ /group/; } } ## use map function @memberof = map { if (/(?<=\=)(.+)/) { my $matched = $1; $matched, $/ if $matched =~ /group/; } } @memberof; print @memberof; [/CODE] Hope this helps. > > TIA, > -- > Groeten, > natxo > -- Tim