Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Loops 需要帮助将结果从ldap解析为csv吗_Loops_Csv_Perl_Ldap_Control Flow - Fatal编程技术网

Loops 需要帮助将结果从ldap解析为csv吗

Loops 需要帮助将结果从ldap解析为csv吗,loops,csv,perl,ldap,control-flow,Loops,Csv,Perl,Ldap,Control Flow,我正在尝试创建一个脚本,用Net::ldap生成一个csv文件,其中包含一些ldap查询的结果,但是如果@attributes数组的一个元素为空,我很难跳过不完整的行 my @attributes = ('cn', 'mail', 'telephoneNumber'); 例如,如果用户未列出邮件或电话号码,则应跳过保留字段,而不是返回: "Foo Bar",, # this line should be skipped since there is no mail nor telephone

我正在尝试创建一个脚本,用Net::ldap生成一个csv文件,其中包含一些ldap查询的结果,但是如果@attributes数组的一个元素为空,我很难跳过不完整的行

my @attributes  = ('cn', 'mail', 'telephoneNumber');
例如,如果用户未列出邮件或电话号码,则应跳过保留字段,而不是返回:

"Foo Bar",, # this line should be skipped since there is no mail nor telephone
"Bar Foo","bar@foo.com", # this line should be skipped too, no number listed
"John Dever","john_dever@google.com","12345657" # this one is fine, has all values
我现在的循环如下所示:

# 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
}
我尝试过如下代码:

if ($attr == "") { next; }
if (length($attr) == 0) { next; }
还有一些人运气不好。我还尝试了简单的if(){print“isempty”}调试测试,但它不起作用。我不太确定我怎么能做到这一点

我很感激你能给我任何帮助或指点我做错了什么

非常感谢你的帮助

更新:
根据请求:

my $singleval = 0;
此程序的示例运行将返回:

Jonathan Hill,Johnathan_Hill@example.com,7883                  
John Williams,John_Williams@example.com,3453                     
Template OAP,,                                            
Test Account,,                                                
Template Contracts,,

因此,我想做的是跳过所有缺少字段的行,无论是电子邮件还是分机号码。

标记您的
,而
循环:

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

next Record;
您的问题是您的
next
与您的
foreach
相关联。使用标签可以避免这种情况


顺便说一句,
$attr=''
,虽然在这种情况下可以工作,但逻辑不好;在perl中,
==
是一种数值比较。字符串比较将是
$attr eq'
。尽管我只想使用
next Record,除非$attr

我尝试了您的建议,即标记while循环并使用next Record,除非$attr,但它仍在打印带有空字段的行。我非常感谢你的建议!我是perl新手,不知道我必须对非数值使用eq而不是==。谢谢你的提示!!!不客气。更仔细地看代码,$attr是一个数组引用,所以它总是正确的。尝试下一个记录,除非@$attr;这也不起作用:(另外,我试过使用if(@$attr eq“”){print“empty”;}但它没有打印为空,所以我认为错误在其他地方。是的。再次阅读您的帖子,看起来好像是,既然您想在有空值时跳过它,那么您需要做的就是替换if($val eq“”){print“empty”;}对于next Record,除非$val;否,否则也不起作用。事实上,我将print语句留在那里,当字段为空时,它不会打印为“空”。我整天都在尝试调试它,但运气不佳。这可能是perl中的错误吗?