2009-10-27

My set complement Perl script.

Currently, my awk-vs-perl knowledge slider is about like this.

awk <--O-------> Perl

I am trying to move it more like this.

awk <-----O---> Perl

To that end, I'll start by translating one of my handy-dandy mini-awk scripts to perl. Have you ever needed to find the elements in one list that were not in another list?

For example, suppose you have a list of all files in a directory. You also have a list of all files in a package, and some of those files in the package list are the ones in the directory list. Now you want to find out what files in the directory are not part of the package.

Here is the awk code:

#!/usr/bin/awk -f

# ** Purpose **
#
# Exclude items in lists A, B, ... from list Z.
#
# ** Usage **
#
# ./exclude_list.awk -v x="listA listB" listZ
#
# The list of files assigned to x is the list of files which contain the lists
# of items to exclude.  The last argument (the file awk processes line by
# line) is the list of items to exclude things from.

BEGIN {
    split(x, ex);
    for (f in ex) {
        while ((getline line < ex[f]) > 0) {
            exclude[line] = 1;
        }
    close(ex[f]);
    }
}

{
    if (exclude[$0] != 1) {
        print;
    }
}

Here is what I just wrote up in Perl.

#!/usr/bin/perl -w

my $set1 = shift;
my %set2=();

while (<>) {
    $set2{$_}++;
}

open my $fh, '<', $set1 or die "Can't open $set1";
while (<$fh>) {
   print $_ if ! exists $set2{$_};
}
close $fh;

To make the source code length comparison fair, I'll have to add POD to the Perl source.

Later on, I'll seek to expand the functionality a little bit, and also see if any Perl built-ins can do this job quicker than I've done here.

2009-10-16

How to determine the gender of Indian names.

I am embarrassed when I accidentally refer to someone named Devangi or Aparna as "he" in an email.  So how do I avoid this?

Simply do a Google image search for the first name.  If you see lots of female faces, then, presto!  It is a woman's name.  You can probably trust the converse result as well.

This is not bigotry on my part.  I am merely trying to avoid referring to women as "he" out of ignorance.  I am very appreciative of Indian culture.

For example, we European-descended folk refer to our numeral system as "Arabic numerals" because that is the culture that introduced them to us, but really they are Hindu numerals.  The Arabs borrowed the idea (including the concept of zero) from the Hindus.  In fact, a lot of Indian mathematics was co-discovered or predates the same European advancement, such a Fibonacci numbers.

Also, where would I be without Yoga?

And Aishwarya Rai truly is the most beautiful woman on Earth.

2009-10-06

Two rpm packaging things I did not know until now.

Sure, there is lots I don't know, but here are the two latest things.  Also, I'm talking about spec files and the creation of rpm packages with rpmbuild.

  1. You can have a verify scriptlet. I never knew that until messing with repackaging Sun's JDK 6.
  2. You can specify what tests to run on files during an rpm --verify operation.

While #1 is interesting, I could have really used #2 on a recent project.