[ic] Splitting a description

Chris Rapier rapier@psc.edu
Sun, 25 Feb 2001 22:29:20 -0500

Victor Nolton wrote:
> Been hunting for help with this all night.
> Need to take the [item-description] and split it in half so I can put
> half the description down one column and the other half down the
> other.
> Basically I guess I'd have to wc the entry, split that number in
> half, if the halfway point lands on a word, move to the end of the
> word then insert a </td><td>
> Has anyone ever done this?

It would be easier to do it in perl. The easy way, but not as precise, 

$line ="The quick brown fox jumps over the lazy dogs";
@words = split /\ /, $line;
$midpoint = int((scalar @words)/2)+1;
for ($i;$i <= $#words; $i++) {
    if ($i == $midpoint) { print "\n";}
    print "$words[$i] ";
print "\n";

I'm sure there is a better way to do this with splice but I'm really too
tired to look it up right now. The way this is set up the 1st line
should generally be a little longer than the 2nd line.

A much more accurate way to do it would be to do this:

$line ="Now is the time that all good men came to the aid of their
@letters = split //, $line;
$midpoint = int((scalar @letters)/2);
for ($i; $i <= $#letters; $i++) {
    if ($i == $midpoint) {
        while ($letters[$i] ne " ") {
          print "$letters[$i]";
        print "\n";
    print "$letters[$i]";
print "\n";

Which does the break down by the total character length of the line. The
trick is the while loop which goes to the mid point and then seeks
forward until it finds a space. Once it finds the space then it prints
the line break but not before it. Again, there is probably a better way
to do this by splices or substrings but again, this took 5 minutes of
thought which is all I can afford to spare at the moment.

Making it work in interchange/minivend is trivial.

Good luck,

Chris Rapier
man of la mancha