Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > more than one set of records separated by blank lines

Reply
Thread Tools

more than one set of records separated by blank lines

 
 
marcorice@gmail.com
Guest
Posts: n/a
 
      07-19-2005
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;
.........

 
Reply With Quote
 
 
 
 
Brian McCauley
Guest
Posts: n/a
 
      07-19-2005
wrote:
> 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;
}
}
}
 
Reply With Quote
 
 
 
 
axel@white-eagle.invalid.uk
Guest
Posts: n/a
 
      07-19-2005
wrote:
> 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 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



 
Reply With Quote
 
marcorice@gmail.com
Guest
Posts: n/a
 
      07-19-2005
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, '') {"

 
Reply With Quote
 
Fabian Pilkowski
Guest
Posts: n/a
 
      07-19-2005
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.

* schrieb:
>
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can one declare more than one signal on one line? Merciadri Luca VHDL 4 11-01-2010 02:00 PM
multiline CSV records (comma-separated values format) Yakov Perl Misc 2 05-12-2007 10:39 PM
Preserve blank lines when add multiple lines of text to a cell Cah Sableng Javascript 0 04-23-2007 04:46 AM
Is it possible to set the Text property of more than one literal in one go? Alan Silver ASP .Net 4 07-03-2005 01:19 PM
Canonical way of dealing with null-separated lines? Douglas Alan Python 17 03-02-2005 01:25 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57