tdsny71 has asked for the wisdom of the Perl Monks concerning the following question:
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#!/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";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Strings overwrite themselves in print command
by hippo (Archbishop) on Jun 27, 2016 at 22:09 UTC | |
by tdsny71 (Initiate) on Jun 28, 2016 at 06:28 UTC | |
|
Re: Strings overwrite themselves in print command
by AnomalousMonk (Archbishop) on Jun 27, 2016 at 22:35 UTC | |
|
Re: Strings overwrite themselves in print command
by BillKSmith (Monsignor) on Jun 28, 2016 at 03:34 UTC | |
by tdsny71 (Initiate) on Jun 28, 2016 at 05:54 UTC | |
by choroba (Cardinal) on Jun 28, 2016 at 08:59 UTC | |
|
Re: Strings overwrite themselves in print command
by Marshall (Canon) on Jun 28, 2016 at 05:37 UTC | |
|
Re: Strings overwrite themselves in print command
by tdsny71 (Initiate) on Jun 27, 2016 at 21:58 UTC |