Hi everyone, a friend recommended I post my problem here, so here goes..... I have a job interview coming up, I hit all the criteria except for one, and the one I'm not hitting is that my perl scripting is rusty as hell. I haven't used it in years, but I'm going to be honest with them and tell them this, but also offer to work on getting it back up to speed in my own time. I think this will be OK as I'm informed that they are looking for people with enthusiasm for scripting, which I do have, it's just that my current role doesn't require any (past roles have required this though). As part of the interview process they have asked me to put together a perl script which compares two text files and outputs any differences. Any functions which compare should not be used so it's basic perl I'm afraid! The data in the text files can be in various orders and there may be duplication of entries. My initial googling on this turned up the following script which I thought looked like a good template for me to start from, but I wanted the advice of the more experienced members on here before moving forward. Am I completely in the wrong ballpark? Obviously I don't want to just copy and paste a perlscript (that would be dumb) but I've had a read through this script and I think it looks pretty close to what I would need. If you think I'm in completely the wrong ball park, please let me know and if you could give me pointers on what I should look at that would be fantastic. Thanks very much! Here's a sample script: #!/usr/bin/perl # file_compare.pl # Purpose: compare two files and show differences # usage: file_compare.pl filename1 filename2 use strict; use warnings; my $file1 = shift or die "filename missing \n"; my $file2 = shift or die "filename missing \n"; open (FILE1, "< $file1") or die "Can not read file $file1: $! \n"; my @file1_contents = <FILE1>; # read entire contents of file close (FILE1); open (FILE2, "< $file2") or die "Can not read file $file2: $! \n"; my @file2_contents = <FILE2>; # read entire contents of file close (FILE2); my $length1 = $#file1_contents; # number of lines in first file my $length2 = $#file2_contents; # number of lines in second file if ($length1 > $length2) { # first file contains more lines than second file my $counter2 = 0; foreach my $line_file1 (@file1_contents) { chomp ($line_file1); if (defined ($file2_contents[$counter2])) { # line exists in second file chomp (my $line_file2 = $file2_contents[$counter2]); if ($line_file1 ne $line_file2) { print "\nline " . ($counter2 + 1) . " \n"; print "< $line_file1 \n" if ($line_file1 ne ""); print "--- \n"; print "> $line_file2 \n\n" if ($line_file2 ne ""); } } else { # there is no line in second file print "\nline " . ($counter2 + 1) . " \n"; print "< $line_file1 \n" if ($line_file1 ne ""); print "--- \n"; print "> \n"; # this line does not exist in file2 } $counter2++; # point to the next line in file2 } } else { # second file contains more lines than first file # or both have equal number of lines my $counter1 = 0; foreach my $line_file2 (@file2_contents) { chomp ($line_file2); if (defined ($file1_contents[$counter1])) { # line exists in first file chomp (my $line_file1 = $file1_contents[$counter1]); if ($line_file1 ne $line_file2) { print "\nline " . ($counter1 + 1) . " \n"; print "< $line_file1 \n" if ($line_file1 ne ""); print "--- \n"; print "> $line_file2 \n" if ($line_file2 ne ""); } } else { # there is no line in first file print "\nline " . ($counter1 + 1) . " \n"; print "< \n"; # this line does not exist in file1 print "--- \n"; print "> $line_file2 \n" if ($line_file2 ne ""); } $counter1++; # point to next line in file1 } }
It's been quite some time since I have written in perl so I probably can't help you with the code. However, I notice there are a lot of if/else's in that script. It may be fine, but they may judge you on writing clean and efficient code in addition to the perl stuff. If it was me, I'd write my own algorithm in pseudocode (english) first and test the logic thoroughly. Once you have the correct algorithm coding it shouldn't be much more than working with the basic perl constructs. Kinda nice of them to let you know ahead of time what they will be asking you to do.