more than one set of records separated by blank lines

Discussion in 'Perl Misc' started by marcorice@gmail.com, Jul 19, 2005.

  1. Guest

    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;
    .........
     
    , Jul 19, 2005
    #1
    1. Advertising

  2. 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;
    }
    }
    }
     
    Brian McCauley, Jul 19, 2005
    #2
    1. Advertising

  3. Guest

    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
     
    , Jul 19, 2005
    #3
  4. Guest

    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, '') {"
     
    , Jul 19, 2005
    #4
  5. 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
     
    Fabian Pilkowski, Jul 19, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alan Silver
    Replies:
    4
    Views:
    533
    Alan Silver
    Jul 3, 2005
  2. Merciadri Luca
    Replies:
    4
    Views:
    841
  3. Cah Sableng
    Replies:
    0
    Views:
    256
    Cah Sableng
    Apr 23, 2007
  4. Steven D'Aprano
    Replies:
    0
    Views:
    134
    Steven D'Aprano
    Dec 23, 2013
  5. Replies:
    3
    Views:
    108
    Gary Herron
    Dec 23, 2013
Loading...

Share This Page