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.
我正在尝试创建一个脚本来生成一个csv文件,其中包含一些使用Net :: LDAP的ldap查询的结果,但是如果@attributes数组的一个元素为空,我就很难跳过不完整的行。
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","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
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.
和其他几个没有运气的人。我也试过简单的if(){print“isempty”;调试测试,它不工作。我不是很确定我怎么能这样做。
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,Johnathan_Hill@example.com,7883
John Williams,John_Williams@example.com,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.
所以我想要做的是跳过所有缺少字段的行,无论是电子邮件还是分机号码。
1 个解决方案
#1
Label your while
loop:
标记你的while循环:
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.
你的问题是你的下一个与你的foreach有关。使用标签可以避免这种情况。
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
.
顺便说一句,$ attr ==''虽然在这种情况下会起作用,但是逻辑错误;在perl中,==是一个数字比较。字符串比较将是$ attr eq''。虽然我只是使用下一个记录,除非$ attr。
#1
Label your while
loop:
标记你的while循环:
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.
你的问题是你的下一个与你的foreach有关。使用标签可以避免这种情况。
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
.
顺便说一句,$ attr ==''虽然在这种情况下会起作用,但是逻辑错误;在perl中,==是一个数字比较。字符串比较将是$ attr eq''。虽然我只是使用下一个记录,除非$ attr。