[interchange-cvs] interchange - heins modified 3 files

interchange-core@icdevgroup.org interchange-core@icdevgroup.org
Tue May 13 14:33:00 2003


User:      heins
Date:      2003-05-13 18:32:25 GMT
Added:     eg/usps  html2tab join123local makezone
Log:
* Add some tweezy scripts that help make US Postal zone charts and
  tables. Use at your own risk.

Revision  Changes    Path
1.1                  interchange/eg/usps/html2tab


rev 1.1, prev_rev 1.0
Index: html2tab
===================================================================
#!/usr/bin/perl

## Turns an HTML table into a tab-delimited table
##
##  usage: html2tab [-c N] < input > output
##
## Used for manually building USPS rate tables from
## their HTML versions.

use Getopt::Std;

getopts('c:');

undef $/;
my $data = <>;

my $max = $opt_c - 1;

$opt_c = undef if $max < 1;

while($data =~ s{<tr.*?>(.*?)</tr>}{}is) {
	my $row = $1;
	my @cells;
	my $i = 0;
	while($row =~ s{<td.*?>(.*?)</td>}{}is) {
		my $col = $1;
		$col =~ s{</?\w.*?>}{}gs;
		$col =~ s/^\s+//;
		$col =~ s/\s+$//;
		push @cells, $col;
		if($opt_c and $i++ >= $max) {
			print join "\t", @cells;
			print "\n";
			@cells = ();
			$i = 0;
		}
	}
	if(@cells) {
		print join "\t", @cells;
		print "\n";
	}
}



1.1                  interchange/eg/usps/join123local


rev 1.1, prev_rev 1.0
Index: join123local
===================================================================
#!/usr/bin/perl

## Turns local,1,2,3 column into separate columss.
##
##  usage: join123local < input > output
##
## Used for manually building Priority mail table from
## output of html2tab.


use Getopt::Std;

getopts('cd:j:');

my $delim = $opt_c ? '\s*,\s*' : "\t";
my $joiner = $opt_c ? ',' : "\t";

$delim = $opt_d if $opt_d;
$joiner = $opt_j if $opt_j;

my @zones = qw/local 1 2 3 4 5 6 7 8/;
print join $joiner, 'weight', @zones;
print "\n";

while(<>) {
	s/\s+$//;
	@f = split /$delim/o, $_;
	print join $joiner, $f[0], $f[1], $f[1], $f[1], @f[1..$#f];
	print "\n";
}



1.1                  interchange/eg/usps/makezone


rev 1.1, prev_rev 1.0
Index: makezone
===================================================================
#!/usr/bin/perl

## Directly turns the output of the USPS zone chart HTML output into
## a valid UPS-style lookup table.
##
##  usage: makezone <input.html  >450.csv
##

use Getopt::Std;

getopts('m:t');

$opt_m ||= 'Priority Book';

my @modes = grep /\S/, split /\s+/, $opt_m;

undef $/;
my $data = <>;

my $max = 1;

my $delim = $opt_t ? "\t" : ',';

print join $delim, 'DestZip', @modes, 'bmc';
print "\n";

sub process_row {
	my ($range, $zone) = @_;
	
	my $bmc = $zone =~ s/\s*\*\s*// ? 1 : 0;

	if($range !~ s/\.+/-/) {
		unless ($range =~ /^\d+$/) {
			warn "Bad zone range=$range, skipping.\n";
			return;
		}
		$range = "$range-$range";
	}
	my @cells = $range;
	for(@modes) {
		push @cells, $zone;
	}
	push @cells, $bmc;
	my $out = join $delim, @cells;
	$out .= "\n";
}

while($data =~ s{<tr.*?>(.*?)</tr>}{}is) {
	my $row = $1;
	my @cells;
	my $i = 0;
	while($row =~ s{<td.*?>(.*?)</td>}{}is) {
		my $col = $1;
		$col =~ s{</?\w.*?>}{}gs;
		$col =~ s/^\s+//;
		$col =~ s/\s+$//;
		push @cells, $col;
		if($i++ >= $max) {
			$line = process_row(@cells)
				and push @out, $line;
			@cells = ();
			$i = 0;
		}
	}
	if(@cells) {
		$line = process_row(@cells)
				and push @out, $line;
	}
}

print sort @out;