Hacking Google Maps and Google Earth (ExtremeTech)

(Dana P.) #1

70 Part I — Basics


Listing 5-4(continued)

print “ID: $id\nRef: $ref\nFirst: $fname\nLast: $lname\nCountry:
$country\n”;
}

close(DATA);

Reading Fixed-Width Files


To extract the information from a fixed-width file, you must read an entire record (say 80
bytes) and then extract individual fields by reading that number of bytes from the file. For
example, you could extract three fields of 20 bytes each and two of 10 bytes each. For the sys-
tem to work, you have to “pad out” the fields with additional bytes to fill up the space (and
remove them when you read them back). For example, you might pad out “Martin” with an
additional 14 bytes to make up the 20 characters for the field width.

The basic operation is to read the number of bytes from the file according to the size of the
record you are reading. In this case, I’ve calculated the record size by adding the width of each
individual field. The unpack()function accepts a string that specifies how to “unpack” a
string from its original format into a sequence of fields. For a fixed-width system, you specify
the width of each field and the character type (in this case ASCII). Listing 5-5 shows a sample
method for reading fixed-width records.

Listing 5-5:Reading Fixed-Width Files in Perl with Unpack

open(DATA,$ARGV[0]) or die “Cannot open file: $!”;

my $reclength = 10+10+20+20+20;
my $record;

while(read(DATA,$record,$reclength))
{
my ($id,$ref,$fname,$lname,$country) = unpack(‘a10a10a20a20a20’,$record);
print “ID: $id\nRef: $ref\nFirst: $fname\nLast: $lname\nCountry:
$country\n”;
}

close(DATA);

The unpack()function in Perl is a great way of extracting the information, but Listing 5-6
shows how you can achieve the same result by using regular expressions.
Free download pdf