Current Issue


Table of contents

CD-ROM

Sys Admin and The Perl Journal CD-ROM version 12.0

Version 12.0 delivers every issue of Sys Admin from 1992 through 2006 and every
issue of The Perl Journal from 1996-2002 in one convenient CD-ROM!

Order now!

Sys Admin Magazine > Archives > 2000 > December 2000

Page artSo What's the Difference?

Randal L. Schwartz

A lot of common programming is dealing with things that change. Things do indeed change, and sometimes we'd like to know how they changed.

For example, if we have a list of items:

@one = qw(a b c d e f g);

then later, we look at it again, and there's a different set of items:

@two = qw(b c e h i j);

How can we tell what's new, what's old, and what's gone? We could certainly try to do it by brute force:

@one = qw(a b c d e f g);
@two = qw(b c e h i j);
foreach $one (@one) {
  if (grep $one eq $_, @two) {
    print "$one is in both old and new\n";
  } else {
    print "$one has been deleted\n";
  }
}
foreach $two (@two) {
  unless (grep $two eq $_, @one) {
     print "$two has been added\n";
  }
}
This in fact gives us an appropriate response:

a has been deleted
b is in both old and new
c is in both old and new
d has been deleted
e is in both old and new
f has been deleted
g has been deleted
h has been added
i has been added
j has been added
But this is incredibly inefficient. The computation time will rise in proportion to the product of sizes of both the lists. This happens because every element of one list is being compared to every element of the other list (twice, in fact). The grep operator is a loop over each item, so we effectively have nested loops, which should usually be a danger sign.
The perlfaq4 manpage approaches this subject, giving a solution of something like:

@union = @intersection = @difference = ();
%count = ();
foreach $element (@one, @two) { $count{$element}++ }
foreach $element (keys %count) {
 push @union, $element;
 push @{ $count{$element} > 1 ? \@intersection : \@difference }, $element;
}
with the caveat that we're assuming one item of each kind within each list.



MarketPlace

Flowcharts from C/C++ code -- Free trial download
Understand C/C++ code in less time. A new team member ? Inherited legacy code ? Get up to speed faster with Crystal Flow for C/C++. Code-formatting improves readability. Flowcharts are integrated with code browser. Export flowcharts to Visio.

Automate Software Builds with Visual Build Pro
Easily create an automated, repeatable process for building and deploying software.

WinDev 12 - Powerful IDE
Develop 10 times faster ! ALM, IDE, .Net, RAD, 5GL, Database, 5GL, 64-bit, etc. Free Express version

Web based bug tracking - AdminiTrack.com
AdminiTrack offers an effective web-based bug tracking system designed for professional software development teams.

Wanna see your ad here?