[comp.lang.perl] Splitting telegrams

carlton@apollo.hp.com (Carlton B. Hommel) (06/10/91)

I ran Jon's awk script through the "a2p" program, to convert it to perl.
It wasn't an improvement.

So, I looked at what his script was trying to do, and rewrote it using
associative arrays.  The approach is novel enough that I'm cross-posting
to comp.lang.perl.  Please followup to the appropriate group, only.

Below is a shar file containing split_telegrams, and, for the comp.lang.perl
readers who have no idea what empire is, a sample input file.

Carl Hommel
carlton@apollo.hp.com

Jonathan Sari writes:
>This splits telegrams.  Save it to a file, and "chmod +x filename".  Then,
>from within empire, you can "read | filename", and your telegrams will be
>appended to "telex.production" "telex.announcement" "telex.bulletins" and
>"telex.telegrams" in the current directory.
>
>It's also available by anonymous ftp to last.cac.washington.edu
>
>-Jon
>|---------------------------------- cut here ------------------------------|
>#! /bin/awk -f
>/^> Product/, /^> [ATB]/ {if (($1 != ">") || (!index("ATB", substr($2, 1, 1))))
>				print >> "telex.production"}
>/^> Announce/, /^> [PTB]/ {if (($1 != ">") || (!index("PTB", substr($2, 1, 1))))
>				print >> "telex.announcement"}
>/^> BULLETIN/, /^> [APT]/ {if ($1 != ">") print >> "telex.bulletins"}
>/^> Telegram/, /^> [ABP]/ {if (($1 != ">") || (!index("ABP", substr($2, 1, 1))))
>				print >> "telex.telegrams"}

#!/bin/sh
# This is a shell archive (produced by shar 3.49)
#
# made 06/10/1991 14:46 UTC by carlton@apollo.hp.com
#
# ============= split_telegrams ==============
if test -f 'split_telegrams' -a X"$1" != X"-c"; then
	echo 'x - skipping split_telegrams (File already exists)'
else
echo 'x - extracting split_telegrams (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'split_telegrams' &&
#!/usr/local/bin/perl
#
# Placed in the public domain by Carl Hommel
X
;#      Keyword in telegram  output file name
%TYPE_TO_FILE = ("Product",  "tele.production",
X                 "Announce", "tele.announce",
X                 "BULLETIN", "tele.bulletins",
X                 "Telegram", "tele.telegrams" );
X
# Open all the output files
foreach $handle (keys %TYPE_TO_FILE) {
X    open($handle, ">>$TYPE_TO_FILE{$handle}") || die "Cannot append to \"$TYPE_TO_FILE{$handle}\": $!\n";
}
X
line:while (<>) {
X    if (/^>/) {
X        search:foreach $handle (keys %TYPE_TO_FILE) {
X            next search if (!/^> $handle/);
X            $output = $handle;
X        }
X    }
X    print $output $_;
}
SHAR_EOF
chmod 0777 split_telegrams ||
echo 'restore of split_telegrams failed'
Wc_c="`wc -c < 'split_telegrams'`"
test 686 -eq "$Wc_c" ||
	echo 'split_telegrams: original size 686, current size' "$Wc_c"
fi
# ============= tele ==============
if test -f 'tele' -a X"$1" != X"-c"; then
	echo 'x - skipping tele (File already exists)'
else
echo 'x - extracting tele (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'tele' &&
read >! tele
> Announcement from POGO, (#0)  dated Fri May  3 13:56:04 1991
Game will shut down tonight at midnight PDT;
back up again Sunday night at midnight PDT.
X-bf
X
> Production Report   dated Fri May  3 17:00:16 1991
X  0 happiness,   0 education produced
total pop is 2004, yielding 0.00 hap, 0.00 edu
0.0000 tech, 0.0000 research produced
money delta was $87 for this update
X
> Telegram from UUCP, (#5)  dated Fri May  3 09:01:46 1991
Are you logged on today?
> Production Report   dated Fri May  3 23:00:15 1991
X  0 happiness,   0 education produced
total pop is 1957, yielding 0.00 hap, 0.00 edu
0.0000 tech, 0.0000 research produced
money delta was $80 for this update
X
> Announcement from POGO, (#0)  dated Mon May  6 08:26:17 1991
Had a path problem in the startup script this morning ...
sorry about that.  I forced an update a few minutes ago
to make up for the one that didn't happen at 5 am.
X-bf
X
SHAR_EOF
chmod 0644 tele ||
echo 'restore of tele failed'
fi
exit 0