David Neven
David Neven

Reputation:

Need help parsing results from ldap to csv

I am trying to create a script to generate a csv file with the results of some ldap queries using Net::LDAP but I'm having troubles skipping incomplete lines if one element of the @attributes array is blank.

my @attributes  = ('cn', 'mail', 'telephoneNumber');

So for example, if a user has no mail listed, or no telephoneNumber listed, then it should skip the hold field instead of returning:

"Foo Bar",, # this line should be skipped since there is no mail nor telephone
"Bar Foo","[email protected]", # this line should be skipped too, no number listed
"John Dever","[email protected]","12345657" # this one is fine, has all values

My loop right now is looking like this:

# Now dump all found entries
while (my $entry = $mesg->shift_entry()){
    # Retrieve each fields value and print it
    # if attr is multivalued, separate each value
    my $current_line = ""; # prepare fresh line
    foreach my $a (@attributes) {
        if ($entry->exists($a)) {
            my $attr = $entry->get_value($a, 'asref' => 1);
            my @values  = @$attr;
            my $val_str = "";
            if (!$singleval) {
                # retrieve all values and separate them via $mvsep
                foreach my $val (@values) {
                    if ($val eq "") { print "empty"; }
                    $val_str = "$val_str$val$mvsep"; # add all values to field
                }
                $val_str =~ s/\Q$mvsep\E$//; # eat last MV-Separator
            } else {
                $val_str = shift(@values); # user wants only the first value
            }

            $current_line .= $fieldquot.$val_str.$fieldquot; # add field data to current line

        }
        $current_line .= $fieldsep; # close field and add to current line
    }
    $current_line =~ s/\Q$fieldsep\E$//; # eat last $fieldsep
    print "$current_line\n"; # print line
}

I have tried code like :

if ($attr == "") { next; }
if (length($attr) == 0) { next; }

and several others without any luck. I also tried simple if () { print "isempty"; } debug tests and its not working. Im not exacly sure how could I do this.

I appreciate any help or pointers you could give me on what am I doing wrong.

Thanks a lot in advance for your help.

UPDATE:
Per chaos request:

my $singleval = 0;

A sample run for this program would return:

Jonathan Hill,[email protected],7883                  
John Williams,[email protected],3453                     
Template OAP,,                                            
Test Account,,                                                
Template Contracts,,

So what I want to do is to skip all the lines that are missing a field, either email or extension number.

Upvotes: 2

Views: 1440

Answers (1)

chaos
chaos

Reputation: 124325

Label your while loop:

Record: while (my $entry = $mesg->shift_entry()){

and use:

next Record;

Your problem is that your next is associated with your foreach. Using the label avoids that.

By the way, $attr == '', though it will work in this case, is bad logic; in perl, == is a numeric comparison. String comparison would be $attr eq ''. Though I'd just use next Record unless $attr.

Upvotes: 2

Related Questions