Keirnan, Paul wrote: > Hi, > > Am fairly new to Perl, so would like some pointers on the correct (elegant) > way of doing the following: > > (It's working, so that is not a problem). > > I am using Net::LDAP to search and update our Novell Edirectory. > One of the attributes returned is an array of "associations". > Under certain circumstances I am removing one of these associations.
To remove an element from an array, you would normally use splice. > My code snippet reads: > > ----------------------------------------------- > use strict; > ... > ... > if ( ... various conditions ... ) { > my @assocs2 = ""; > foreach (@assocs) { > if (!/cn=DudDriver/) { > if (!$assocs2[0]) { > $assocs2[0] = $_; > } > else > { > $assocs2 [@assocs2] = $_; > } > } > } > $person->replace("DirXML-associations" => [@assocs2]); > $person->update( $eDir); > die "\nModify failed (" . ldap_error_name($mesg->code) . ")\n" > if $mesg->code; > } > ... > ----------------------------------------------- > > Now I would like to code that as > > my @assocs2 = ""; > foreach (@assocs) { > if (!/cn=DudDriver/) { > $assocs2 [@assocs2] = $_; Not sure what you're trying to do there. Maybe: $assocs2[0] = $_; or possibly $assocs2[$assocs2[0]] = $_; depending on what you're trying to do. > } > } > > but that doesn't populate the first entry correctly because > $ASSOCS2[0] = "" (as shown by 'x' in debug) > prior to entering the loop. > > So far as I am aware, I need to define the array outside the 'foreach' > loop so that I can use it in the 'replace' statement. > > So what is the accepted method of populating that first element of the > array? That's scary looking code. An array is initialized to empty with: my @assocs2 = (); So to set the first element you would do: my @assocs2 = ('first element'); or my @assocs2 = (100); kinda thing. -- ,-/- __ _ _ $Bill Luebkert ICQ=162126130 (_/ / ) // // DBE Collectibles Mailto:[EMAIL PROTECTED] / ) /--< o // // http://dbecoll.tripod.com/ (Free site for Perl) -/-' /___/_<_</_</_ Castle of Medieval Myth & Magic http://www.todbe.com/ _______________________________________________ Perl-Unix-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs