[lug] parsing between two lists
Tkil
tkil at scrye.com
Thu Mar 28 14:34:16 MST 2002
>>>>> "Tkil" == tkil <tkil at scrye.com> writes:
| my $first = <FIRST>;
| my $second = <SECOND>;
|
| while (defined($first) && defined($second))
| {
| if ($first lt $second)
| {
| print IN_FIRST $first;
| $first = <FIRST>;
| }
| elsif ($first gt $second)
| {
| print IN_SECOND $second;
| $second = <SECOND>;
| }
| else
| {
| print IN_COMMON $first;
| $first = <FIRST>;
| $second = <SECOND>;
| }
| }
|
| # take care of stragglers
| if (defined($first))
| {
| while ($first = <FIRST>) { print $first; }
| }
| else
| {
| while ($second = <SECOND>) { print $second }
| }
that last bit is probably wrong. consider:
first: a b c d f g
second: a b c d e
the first four get caught by the "common" case at the top. after
that, $second is undef, so it drops out. $first is still defined (to
be "f"), so we drop into the first straggler case. and there's the
problem: we immediately overwrite $first with the next letter ("g").
huh. i forgot to send the output to the appropriate filehandles, at
that. bad tkil, no donut.
so, we want something more like this:
| # take care of stragglers
| if (defined($first))
| {
| do
| {
| print IN_FIRST $first;
| }
| while ($first = <FIRST>);
| }
| else
| {
| do
| {
| print IN_SECOND $second;
| }
| while ($second = <SECOND>);
| }
mm... this nugget is quite shiny by now, ain't it?
t.
More information about the LUG
mailing list