I am trying to make a CSV file out of an LDAP output that comes from an external process.

Here is the structure of the output file:

cn: applegroup
member: CN=mem001,OU=Users,DC=myorg,DC=com
member: CN=mem002,OU=Users,DC=myorg,DC=com
member: CN=mem003,OU=Users,DC=myorg,DC=com
member: CN=mem004,OU=Users,DC=myorg,DC=com

cn: orangegroup
member: CN=mem301,OU=Users,DC=myorg,DC=com
member: CN=mem302,OU=Users,DC=myorg,DC=com
member: CN=mem303,OU=Users,DC=myorg,DC=com

And here is the code I have to read it:
#!/usr/bin/perl use strict; use warnings; # 0a) Initialize variables my $grpNm = ""; my $memID = ""; my @groupInfo = ("", ""); my @memberInfo = ("", ""); my @ldapMemInfo = ("", ""); my @idInfo = ("", ""); my @csvContent = ("", ""); my $allGroupResult = "allTmp.txt"; my $allGroupDataFinal = "allData.csv"; open my $fhGroupFile, '<', "$allGroupResult" or die "Could not create +or open $allGroupResult"; open my $fhCSVOutput, '>', "$allGroupDataFinal" or die "Could not crea +te or open $allGroupDataFinal"; sub lTrim { my $s=shift; $s =~ s/^\s+//; return $s; } sub rTrim { my $s=shift; $s =~ s/\s+$//; return $s; } sub trimAll { my $s=shift; $s =~ s/^\s+|\s+$//g; return $s; } # ------------ Main Program -------------------- while (<$fhGroupFile>) { my $line = $_; chomp $line; # Check if line begins with "cn" or "member" if ($line =~ m/^cn/) # found group name { @groupInfo = split /:/, $line; $grpNm = $groupInfo[1]; # second entry is group name $grpNm = lTrim($grpNm); #trim leading spaces from group name # print "array indice $groupInfo[1]\n"; print "scalar grpNm:$grpNm\n"; $csvContent[0] = $grpNm; } elsif ($line =~ m/^member/) # found member name { @memberInfo = split /:/, $line; @ldapMemInfo = split /,/, $memberInfo[1]; # second entry is full + string for ldap content @idInfo = split /=/, $ldapMemInfo[0]; # second entry is ldap con +tent #print "array idInfo /= @idInfo\n\n"; $memID = $idInfo[1]; # second entry is the specific ID #print "scalar memID = $memID\n"; $csvContent[1] = $memID; print $fhCSVOutput "$csvContent[0]"; print $fhCSVOutput ","; #print $fhCSVOutput "$csvContent[1]"; print $fhCSVOutput "\n"; } else { } } # while <$fhGroupFile> close($fhGroupFile); close($fhCSVOutput);

When I run it I get:
,pplegroup
,pplegroup
,pplegroup
,pplegroup
,rangegroup
,rangegroup

Before you ask, I get the same result if I use the scalar variables without the array @csvContent so,

print $fhCSVOutput "$csvContent[0]"; print $fhCSVOutput ","; #print $fhCSVOutput "$csvContent[1]"; print $fhCSVOutput "\n";

AND

print $fhCSVOutput "$grpNm"; print $fhCSVOutput ","; #print $fhCSVOutput "$memID"; print $fhCSVOutput "\n";

yield the same erroneous data.

In reply to Strings overwrite themselves in print command by tdsny71

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.