more than one set of records separated by blank lines

M

marcorice

I am new to perl and have had this problem for a while now.
I'm doing an ldapsearch with the following result of a record set.

CN=junk
mail=emailaddess
employeeID=somenumber
givenName=first_name
middleName=middle_name
sAMAccountName=somnumber_or_string
sn=last_name
telephoneNumber=telephone_number
userPrincipalName=some_email_string
extensionAttribute13=somenumber

Sometimes I get more than one set of records separated by blank lines.
I need to do somekind of loop that evaluates each set separately.

chomp(@info= (`ldapsearch -p $PORT -b DC=com -s sub -h $SHOST
"(&(sn=$lname)(givenName=$fname))" givenName middleName sAMAccountName
sn
employeeID mail telephoneNumber extensionAttribute13
userPrincipalName`));
foreach $list (@info) {
if ($list =~ s/extensionAttribute13=//){
$ntuid = $list;
}
elsif ($list =~ s/telephoneNumber=//){
$telenum = $list;
.........
 
B

Brian McCauley

I am new to perl and have had this problem for a while now.
I'm doing an ldapsearch with the following result of a record set.

CN=junk
mail=emailaddess
employeeID=somenumber
givenName=first_name
middleName=middle_name
sAMAccountName=somnumber_or_string
sn=last_name
telephoneNumber=telephone_number
userPrincipalName=some_email_string
extensionAttribute13=somenumber

Sometimes I get more than one set of records separated by blank lines.
I need to do somekind of loop that evaluates each set separately.

chomp(@info= (`ldapsearch -p $PORT -b DC=com -s sub -h $SHOST
"(&(sn=$lname)(givenName=$fname))" givenName middleName sAMAccountName
sn
employeeID mail telephoneNumber extensionAttribute13
userPrincipalName`));
foreach $list (@info) {
if ($list =~ s/extensionAttribute13=//){
$ntuid = $list;
}
elsif ($list =~ s/telephoneNumber=//){
$telenum = $list;
........

I shall assume that any line without an equals-sign separates records.

{
my $record;
for (@info, '') {
if ( /(.*?)=(.*)/ ) {
$record->{$1} = $2;
} elsif ( $record ) {
# do stuff with $record
undef $record;
}
}
}
 
A

axel

I am new to perl and have had this problem for a while now.
I'm doing an ldapsearch with the following result of a record set.

Sometimes I get more than one set of records separated by blank lines.
I need to do somekind of loop that evaluates each set separately.
chomp(@info= (`ldapsearch -p $PORT -b DC=com -s sub -h $SHOST
"(&(sn=$lname)(givenName=$fname))" givenName middleName sAMAccountName
sn
employeeID mail telephoneNumber extensionAttribute13
userPrincipalName`));
foreach $list (@info) {
if ($list =~ s/extensionAttribute13=//){
$ntuid = $list;
}
elsif ($list =~ s/telephoneNumber=//){
$telenum = $list;
........

I notice you are using the command line ldap tools.

May I suggest the Net::LDAP module available from CPAN - it may
simplify your life considerably.

Axel
 
M

marcorice

Thank you for the code, I am so close but right now if i do a print
"$record->{$1}\n"; or print "$1 or $2" under the elsif condition, all
I get is the last line of each record set. I want to be able to call
up more than the last line, your code is definitely the route that I
need. Also can you explain this "for (@info, '') {"
 
F

Fabian Pilkowski

Please learn how to quote. This is no Google Group where each one could
see all other posts in front of this one. This is Usenet. You have to
quote the parts you answering to (then its easier to see whom you answer
than searching for the previous posting). But please, not the whole
posting rather than the parts you really answering to. Read some other
posts of this group to see how others do it.

I know Google is not the best interface to access Usenet but they allow
to draw up a posting with proper quoting. Please use it.

Thank you for the code, I am so close but right now if i do a print
"$record->{$1}\n"; or print "$1 or $2" under the elsif condition, all
I get is the last line of each record set.

If you run into the elsif-part you know, that the regex didn't match.
Hence the special vars $1 and $2 are still set to their last match (the
line before containing an equal sign). You have to read it as follows:
If there is an equal sign, save both values into $record. Later you
could access these values via $record rather than via $1 and $2. Try to
replace the "# do stuff with $record" in Brian's Code by something like

print $record->{mail};
print $record->{telephoneNumber};

You'll see that all your value are stored in $record. Read something
about hashes and references to learn how the data is stored here.
Also can you explain this "for (@info, '') {"

You have to guarantee that the last element contains no equal sign
(otherwise the elsif-part is never called for the last item). It's just
for iterating over all elements of @info plus one additional element (an
empty string). Think of it as an abbreviation for

push @info, '';
for ( @info ) { ... }

regards,
fabian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top