in reply to Comparing two files line by line and exporting the differences from the first file

Other monks have posted solutions based on Text::CSV to which I think you should pay close attention. This post is not about the OPed code per se, but about a general approach to debugging code.

Are you using warnings and strict with your code? I suspect not. If not, do so (see example code below), then fix the problems these thinking-aids reveal. These modules are useful for all Perl programmers, but especially for novice Perlers.

After being sure warnings and strict are enabled, the next thing to do is to be sure you are getting the data you think you're getting.

The statement
    ($samaccountnameAD,$givennameAD,...,$managerAD)=split(/,$/);
splits a string on a comma that is at the end of the string. The  $ in the  /,$/ regex is an end-of-string anchor; see perlre, perlretut, and perlrequick. You cannot get more than two fields from this split, but you're trying to get quite a few fields.

c:\@Work\Perl\monks>perl -wMstrict -le "use warnings; use strict; ;; use Data::Dumper; ;; $_ = 'vv,WWWW,xxx,YY,zzzz,'; ;; my ($v, $w, $x, $y, $z) = split(/,$/); print Dumper($v, $w, $x, $y, $z); ;; print qq{'$v' '$w' '$x' '$y' '$z'}; " $VAR1 = 'vv,WWWW,xxx,YY,zzzz'; $VAR2 = ''; $VAR3 = undef; $VAR4 = undef; $VAR5 = undef; Use of uninitialized value in concatenation (.) or string at -e line 1 +. Use of uninitialized value in concatenation (.) or string at -e line 1 +. Use of uninitialized value in concatenation (.) or string at -e line 1 +. 'vv,WWWW,xxx,YY,zzzz' '' '' '' ''
You see in this example the exact output of the split operation; probably not what you wanted and expected. Try this example again with a string that does not end in a comma character; there is a tiny but significant difference. Try it with an empty string as input.

The example above uses Data::Dumper. This utility for visualizing data can be a sanity-saver. It is a core module (i.e., has been made a part of the standard Perl distribution; see corelist for getting info on core modules). I prefer Data::Dump, but it is not core.

This post addresses just one, small aspect of debugging; there are many more. Good luck.


Give a man a fish:  <%-{-{-{-<

  • Comment on Re: Comparing two files line by line and exporting the differences from the first file
  • Select or Download Code