contrite_newbie has asked for the wisdom of the Perl Monks concerning the following question:

Good evening,

I am trying to finish up a script that simply reads 2 files, matches a value unique in each file, and then writes output to a fixed format based on attributes from each file.

Here is the code, and please bear with me. I know that I have major syntax problems, but I think I ended up moving code accidentally when editing.

How do I get this to sort and print properly? I think I am on the right track but I am now getting confused.

#!/usr/bin/perl # # #Creation date: 10/4/2004 # This script will perform four major operations # 1) Read an LDIF extract from Active Directory, # objectClass=Users. Copy DN and samACCOUNTNAME into a hash. # $key is dn and $value is samACCOUNTNAME # 2) Read a CSV extract from a legacy AS400 Database into @hris. # 3) Compare hash and @hris and match the samACCOUNTNAME # value and and value [0] from @hris. On match and write record co +ntaing DN, # samACCOUNTNAME from @mlf1 and Division and Location from # @hris. # 4) Write record to file an LDIF record using the DN, samACCOUNTNAME, + # division,and personalTitle attributes. use warnings; use strict; open (my $FILE, 'c:/test/ldif.ldf') || die "can't open 'ldif.ldf': $!" +; # open LDIF file for reading open (my $OUTPUT_FILE, '>mlfexport.txt') || die "can't open 'mlfexport +.txt': $!"; #open output file open (my $FILE1, 'c:/test/hris.csv') || die "can't open 'hris.csv': $! +"; #open csv file for reading local $/ = "\n\n"; # Set an empty newline as line separator my %lookup; #hash for $File my($key, $value); my $dn = $key; # want to assign the dn as the key in the hash table my $samACCOUNTNAME = $value; #want to assign samACCOUNTNAME to value #next block will hash $file for 2 values while (<$FILE>) { my ($dn)= /^dn: (.*)$/m; my ($samACCOUNTNAME)= /^sAMAccountName: (.*)$/m;{ $lookup {$dn} = {$samACCOUNTNAME} # verify syntax, is $looku +p defined right, and is this matching dn and samACCOUNTNAME if defined $samACCOUNTNAME and $dn ; # only if both are found } } # I think I am making this too complicated. Do I need all these varia +ble declarations? my @hris = ($FILE1); #associate csv file to array my @ldif = (%lookup); #associate table to array my $uid = ((@hris)[0]); #set usernamme which also equals samACCOUNTNAM +E my $div = ((@hris)[1]); #set division value for printing my $loc = ((@hris)[2]); #set personalTitle value for printing # The next block should look for the samACCOUNTNAME in $value and matc +h it to array position 0 in hris. while (<$FILE1>) { foreach my $value (keys %lookup){ if $hris [1] eq $lookup($value) # what I want to do here is val +idate a match on $value and array [0] and print the entry to the outp +ut file } &print_entry($dn, $uid, $div, $loc); } #this block prints the output sub print_entry { local($dn, $uid, $div, $loc) = @_; #print out LDIF file print $OUTPUT_FILE "dn:$ldif[2]","\n"; print $OUTPUT_FILE "changetype:=modify","\n"; print $OUTPUT_FILE "sAMAccountName:=$uid","\n"; print $OUTPUT_FILE "division:=$hris[1]","\n"; print $OUTPUT_FILE "personalTitle:=$hris[2]","\n"; print $OUTPUT_FILE "\n"; } close($OUTPUT_FILE) || die "can't close mlfexport.txt: $!"; close($FILE1) || die "can't close 'hris.csv': $!"; close($FILE) || die "can't close 'ldif.ldf': $!"; exit; # successful exit

Here is error messaging I get when I run it

C:\test>perl mlfimport.pl syntax error at mlfimport.pl line 55, near "if $hris " Global symbol "$lookup" requires explicit package name at mlfimport.pl + line 55. Execution of mlfimport.pl aborted due to compilation errors.

And here is what output I am getting when I have gotten this running with warnings

dn:CN=Patel\, Rupal,OU=Users,OU=BartorRd,OU=Corporate,DC=oakleaf,DC=ca changetype:=modify sAMAccountName:=HASH(0x224e0c) division:= personalTitle:=
Any help would be greatly appreciated.

Replies are listed 'Best First'.
Re: Syntax and variable problems
by pg (Canon) on Oct 08, 2004 at 03:50 UTC

    I tried to perl -c your code, and resolved the immediate issue that cause the error message you mentioned:

    C:\test>perl mlfimport.pl syntax error at mlfimport.pl line 55, near "if $hris " Global symbol "$lookup" requires explicit package name at mlfimport.pl + line 55. Execution of mlfimport.pl aborted due to compilation errors.

    Here is what it should look like:

    while (<$FILE1>) { foreach my $value (keys %lookup) { if ($hris [1] eq $lookup{$value}) { &print_entry($dn, $uid, $div, $loc); } } }

    I didn't resolve everything for you, it is better for you to spend some effort.

    You really should make your code tidy. Messy code does nothing other than confuse yourself, and make debug slower.

      Thanks for the help. Time to soldier on