Net::LDAP を使用していくつかの LDAP クエリの結果を含む csv ファイルを生成するスクリプトを作成しようとしていますが、@attributes 配列の 1 つの要素が空白の場合、不完全な行をスキップするのに問題があります。
my @attributes = ('cn', 'mail', 'telephoneNumber');
たとえば、ユーザーがリストされているメールや電話番号を持っていない場合、次の値を返す代わりに hold フィールドをスキップする必要があります。
"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,,
だから私がやりたいことは、電子メールまたは内線番号のいずれかのフィールドが欠落しているすべての行をスキップすることです.