views:

216

answers:

1

Hi,

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.

+2  A: 

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.

chaos
I tried your suggestion of Labeling the while loop and using next Record unless $attr but it is still printing those lines with empty fields.I appreciate your suggestions very much! I am new to perl and was not aware that I had to use eq instead of == for non numeric values. Thanks for the tip!!!
You're welcome. Looking at your code more closely, $attr is an array reference, so that will always be true. Try next Record unless @$attr;
chaos
that didnt work either :(also, i tried doing if (@$attr eq "") { print "empty"; } and its not printing empty so I think the error is somewhere else.
Yeah. Rereading your post again, it looks like, since you want to skip it if there are any empty values, all you need to do is replace if ($val eq "") { print "empty"; } with next Record unless $val;
chaos
nope, that doesn't work either.in fact, i left the print statement there and its not printing "empty" when a field is empty. i've been all day trying to debug this with no luck. could it be a bug in perl?
No, Perl is fine. The problem seems to be that neither you nor I seem to know how to identify the condition that a value in the list is empty. Could you edit your post to include the output of a run? If we can figure out what to tell the code to look for, that should be the whole ball game.
chaos
It'd also be nice to know whether this $singleval variable is on or not.
chaos