CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Advanced:
2 loops in one

 



rfransix
Novice

May 25, 2010, 11:57 AM

Post #1 of 6 (3030 views)
2 loops in one Can't Post

Please pardon the obscure example, hoping it makes sense to the right person. I'm programming in Windows shell command, and get it to work, so I'm thinking a perl expert can see the logic and help me out with the perl code. Thanks.

Here's the code that does not work to build an ldif import file. I have 2 files, line for line they match up. One file has all the DN's, the other file has the employeeNumber of each DN. I can't get the two FOR loops to iterate through 2 files one line at a time to build the correct ldif entry, and then proceed through both files iteratively until the last lines. Pirate

@echo on

::Set BATCH Input Directory
set batchdir=e:\meta

::Set the input file containing the list
set infile=%batchdir%\DDNs3
set infile2=%batchdir%\DDNsUid2

::If exists, we remove output file
rm DDNs3.ldif

::For loop below process each line in the input list.

:uid
FOR /F "tokens=* delims=" %%i IN (%infile%) do (
echo dn: %%i
echo changetype: modify
echo replace: employeeNumber
call :loop2
)
goto :eof
:loop2
FOR /F "tokens=* delims=" %%k IN (%infile2%) do (
echo employeeNumber: %%k
echo.
call :uid
)

:eof


FishMonger
Veteran / Moderator

May 25, 2010, 1:51 PM

Post #2 of 6 (3023 views)
Re: [rfransix] 2 loops in one [In reply to] Can't Post

Post your Perl script and any errors/warnings that it produces.


(This post was edited by FishMonger on May 25, 2010, 1:52 PM)


rfransix
Novice

May 25, 2010, 6:48 PM

Post #3 of 6 (3017 views)
Re: [FishMonger] 2 loops in one [In reply to] Can't Post

Here's what I have so far, not working of course. Thanks for helping. Crazy

#!/usr/bin/perl



die "Usage: $0 file1 file2\nwhere file1 and file2 are files with equal

number of lines.\n" if @ARGV < 2;



#FILE1 contains DNs, one per line

#FILE2 contains employeeNumbers, one per line



open(FILE1, $ARGV[0]);

open(FILE2, $ARGV[1]);



while ($line = <FILE1>) {

$line =~ s/\n//;

print STDOUT dn: $line

print STDOUT changetype: modify

print STDOUT replace: employeeNumber

print STDOUT employeeNumber <FILE2>

}





#Resultant file should look like this:

Dn: uid=Richard,dc=corp,dc=com

Changetype: modify

Replace: employeeNumber

employeeNumber: 123456789


FishMonger
Veteran / Moderator

May 26, 2010, 6:13 AM

Post #4 of 6 (2994 views)
Re: [rfransix] 2 loops in one [In reply to] Can't Post

Please use the code tags whenever you post code.

Start by adding these 2 lines, which should be in every Perl script you write.

Code
use strict; 
use warnings;

Those pragmas will point out lots of coding errors that can be difficult to track down.

The strict pragma forces you to declare your vars, which is done with the 'my' keyword.

You should always check the return code of an open call to make sure it was successful and take action if it wasn't.

It's best to use the 3 arg form of open and a lexical var for the filehandle instead of the bareword.


Code
open my $file1, '<', $ARGV[0] or die "failed to open '$ARGV[0] $!"; 
open my $file2, '<', $ARGV[1] or die "failed to open '$ARGV[1] $!";



Code
$line =~ s/\n//;

That is normally written as:

Code
chomp $line;


Since the print function is a list operator, your attempt to read in the employee number from file to in the print statement will slurp and print the entire file. Instead, you should assign the employee number to a scalr var and use that var in the print statement.


rfransix
Novice

May 26, 2010, 9:03 AM

Post #5 of 6 (2991 views)
Re: [FishMonger] 2 loops in one [In reply to] Can't Post

Thanks for the advice. I'm working on getting this code working, stay tuned: Wink

#! perl -sw

use strict;

use constant batchdir => "e:\meta";

use constant {

infile => batchdir . "\DDNs3";

nfile2 => batchdir . "\DDNsUid2";

outfile => "DDNs3.ldif";

};



unlink outfile; ## If there delete it; silently do nothing if not.



open DN, '<', infile or die $!;

open UID, '<', infile2 or die $!;

open OUT, '>', outfile or die $!;



while( <DN> ) {

chomp;

print OUT "dn: $_";

print OUT "changetype: modify";

print OUT "replace: employeeNumber";

chomp( my $empNo = <UID> );

print OUT "employeeNumber: $empNo";

};



close OUT;

close UID;

close DN;




rfransix
Novice

May 26, 2010, 10:21 AM

Post #6 of 6 (2980 views)
Re: [rfransix] 2 loops in one [In reply to] Can't Post

RESOLVED SmileWinkCoolLaughSly

With a bunch of corrections, I got this working. All 120000 entries were correctly printed to the ldif file. Any suggestions on improvement are very welcome. Thanks everyone for looking and FishMonger for the guidance, very helpful.

#! perl -slw
use strict;
use warnings;
use constant batchdir => "c:\temp1";

open DN, "<DDNs3" or die $!;
open UID, "<DDNsUid2" or die $!;
open OUT, ">DDNs3.ldif" or die $!;

while( <DN> ) {
chomp;
print OUT "dn: $_";
print OUT "changetype: modify";
print OUT "replace: employeeNumber";
chomp( my $empNo = <UID> );
print OUT "employeeNumber: $empNo\n";
};

close OUT;
close UID;
close DN;

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives