head	1.2;
access;
symbols
	tcllib-1-13:1.2
	tcllib-1-12:1.2
	tklib-0-5:1.2
	tcllib-1-11-1:1.2
	tcllib-1-11:1.2
	tcllib-1-10:1.2
	tcllib-1-9:1.2
	tcllib-1-8:1.2
	tcllib-1-7:1.2
	tcllib-1-6-1:1.1.6.1
	tcllib-1-6-branch:1.1.0.6
	tcllib-1-6:1.1
	tcllib-1-4-0:1.1
	tcllib-1-3-0:1.1
	tcllib-1-2-0:1.1.0.4
	RELEASES:1.1.0.2
	tcllib-1-1-0:1.1
	tcllib-1-0-0:1.1;
locks; strict;
comment	@# @;


1.2
date	2004.03.09.08.20.17;	author andreas_kupries;	state Exp;
branches;
next	1.1;

1.1
date	2001.05.01.19.01.23;	author andreas_kupries;	state Exp;
branches
	1.1.6.1;
next	;

1.1.6.1
date	2004.05.24.02.58.09;	author andreas_kupries;	state Exp;
branches;
next	;


desc
@@


1.2
log
@Unified the startup header of all applications, using
suggestions made by Stuart Cassof <stwo@@telus.net>.
@
text
@#! /bin/sh
# -*- tcl -*- \
exec tclsh "$0" ${1+"$@@"}

# Join two CSV files by key

package require csv
package require cmdline

# ----------------------------------------------------
# csvuniq ?-sep sepchar? keycol1 file1.in keycol2 file2.in file.out|-
#
# Argument processing and checks.

set sepChar ,
set outer 0

set usage "Usage: $argv0 ?-sep sepchar? ?-outer? key1 file1.in key2 file2.in file.out|-"

while {[set ok [cmdline::getopt argv {sep.arg outer} opt val]] > 0} {
    #puts stderr "= $opt $val"
    switch -exact -- $opt {
	sep   {set sepChar $val}
	outer {set outer 1}
    }
}
if {($ok < 0) || ([llength $argv] != 5)} {
    puts stderr $usage
    exit -1
}

foreach {keyA inA keyB inB out} $argv break

if {
    ![string is integer $keyA] ||
    ($keyA < 0)                ||
    ![string is integer $keyB] ||
    ($keyB < 0)                ||
    ![string compare $inA  ""] ||
    ![string compare $inB  ""] ||
    ![string compare $out ""]
} {
    puts stderr $usage
    exit -1    
}

if {![string compare $out -]} {
    set out stdout
} else {
    set out [open $out w]
}

set inA [open $inA r]
set inB [open $inB r]

# ----------------------------------------------------
# Actual processing, uses the following information from the
# commandline:
#
# inA     - channel for input A
# inB     - channel for input B
# out     - channel for output
# sepChar - separator character
# keyA    - key column in A
# keyB    - key column in B

# 1. Read input B completely into an array indexed by the contents of
#    the key column. Store only the non-key information of input
#    B. Note that B may contain several lines having the same key.
#
# 2. Read input A line by line and match its key information against
#    the array. If there is no match ignore the record, else join the
#    record with all records from the array and write the resulting
#    records into the output.

set bwidth 0

array set map {}
while {![eof $inB]} {
    if {[gets $inB line] < 0} {
	continue
    }

    set data [::csv::split $line $sepChar]
    set key  [lindex   $data $keyB]
    set data [lreplace $data $keyB $keyB]

    if {[info exists map($key)]} {
	lappend map($key) $data
    } else {
	set map($key) [list $data]
    }
    set bwidth [llength $data]
}
close $inB

while {![eof $inA]} {
    if {[gets $inA line] < 0} {
	continue
    }
    set data [::csv::split $line $sepChar]
    set key  [lindex   $data $keyA]

    if {[info exists map($key)]} {
	foreach record $map($key) {
	    set res $data
	    eval lappend res $record
	    puts $out [::csv::join $res $sepChar]
	}
    } elseif {$outer} {
	# Nothing was found, but an outer join was requested too =>
	# append 'bwidth' empty cells to the data and write the new
	# record.

	for {set i 0} {$i < $bwidth} {incr i} {
	    lappend data {}
	}

	puts $out [::csv::join $data $sepChar]
    }
}

exit ; # automatically closes the channels
@


1.1
log
@2001-05-01  Andreas Kupries <andreas_kupries@@users.sourceforge.net>

	* Makefile.in (MODULES):  Added module 'report'.

	* all.tcl: Added code to propagate "::tcltest::testDirectory" into
	  the slave actually doing the tests. This tripped some of the
	  tests for the new CSV module as they use some external files and
	  were thus unable to find them correctly without this setting.

	* Makefile.in (MODULES): Added module 'csv'.

	* Added directory 'examples' for future sample applications of
	  tcllib and some example applications too.

	* Added "matrix" data structure to module "struct".
@
text
@d1 4
a4 3
#!/bin/sh
# use -*- tcl -*- \
exec tclsh "$0" "$@@"
@


1.1.6.1
log
@Downgraded to version 1.3.6, removed -decode extension from
this branch.

Import of ftpd bugfix by Gerald Lester.

Last commit was a bad update, caused duplicates of changes
to appear. Failed testsuite. Removed all the duplicates now.

Fixed SF Tcllib Bug 954328. Mime now adapts at runtime to
whatever version of md5 has been loaded.

Updated test for rewritten adjust which fixed the infinite
looping demonstrated by tests 2.6 and 2.7. Also fixed a var
usage typo which caused a copy of the input to appear in the
output, before the expected formatted result.

Fixed bug in the processing of multi-word section titles for
text based formats.

Fixed bug 951568, regarding the usage of Trf's generic
transform.

Fixed problems with jpeg recognition (was unable to detect a
jpeg file, if it contained exif data).

Changelog for last patch, and updates in related package.

Completed application of code for various fixes.

Rewritten text adjustment and hyphenation, fixing SF TCllib
Bug 882402.

Fixed SF Tcllib Bug 936064, and evals more robust.

Fixed SF Tcllib Bug 893516

Fixed SF Tcllib Patch 763712

Fixed SF Tcllib Patch 758742

Fixed SF Tcllib Bug 620852

Eval usage made more robust and similar.

Fixed SF Tcllib Bug 943146.

Fixed SF Tcllib Bug 940651

SF Tcllib Bug 784519 fixed.

Pat: sak.tcl update for better use of critcl.

Joe: Fix in doctools xml support.

Import bugfix by Pat Thoyts, Handling of data starting with
hyphen/dash

Import of uuencode changes by Jeff Hobbs.

Changed defaults for package 'log'. No output for the all
levels below 'error'.

Unified the startup header of all applications, using
suggestions made by Stuart Cassof <stwo@@telus.net>.

Added testcase for Tcllib SF Bug 860753. The bug itself was
already fixed for Tcllib 1.6.

Fix for bug 899204. Test data file is opened read-only, and
tests made independent of each other.

Bugfix 899152, 899209. Require Tcl 8.2 for installer, delete
file before writing over it.

Import of time fix by Pat Thoyts, patch #905132.

Cleanup fix: Snit depends on Tcl 8.4, this is documented,
however neither package index, nor testsuite enforced the
restriction, allowing for errors. This has been changed now.

Fixed typos
@
text
@d1 3
a3 4
#! /bin/sh
# -*- tcl -*- \
exec tclsh "$0" ${1+"$@@"}

@


