krujos has asked for the wisdom of the Perl Monks concerning the following question:

I seem to keep running into the same problem, and as of yet I have not come up with a good way to solve it. Basically here is the deal. I get an some input data. Usually something to the effect of a bunch of lines that include category A and category B and some other junk. Usually there are six or eight values for category A and six or eight for category B (sometimes there is more sometime there is less last time I had to do this there were six and eight so the numbers are fresh). so far we have data that looks like this.
 CatA:CatB:something:else:andSuch
What I need to end up with is a grid with A cross referenced with B. so ideally something like this.
|______A_______________________________________ B | the value of some other field goes in here |

Sorting the keys would be nice, but that seems like it would be the easy part. Basically the main thing I am trying to do in all of my attempt at this is to print stuff out in nice easy to read grid format.
So far I have ran into this problem three times in the last three years , once in a class and twice at work. As of yet I have not come up with a solution I like yet. They all seem cludgy and more complicated than I like them to be.
My solutions so far have been.
1. a multi dimensional array
2. the same idea but with hashes because I was using strings I wanted to look them up by name rather than an array index.
3. I built a rather shoddy object which jeffa helped me to make much better, but the solution to the problem still sucked.(my solution not his)

The thing with all of these solutions is that they all seem to suck and use about 50 times more indirection / smoke and mirrors than need be. Anyone have any suggestions before this comes back to haunt me next year?
here is the code for the best attempt yet
Thanks
Josh

Replies are listed 'Best First'.
Re: Changing the way data is formatted, a three year conundrum
by tadman (Prior) on May 07, 2002 at 06:57 UTC
    Just off the top:
    foreach (@stuff) { my ($a,$b,@stuff) = split (/:/); push (@{$data{$a}{$b}}, @stuff); }
    Or, if you are working with unique A:B pairs, you could do something like this:
    foreach (@stuff) { my ($a,$b,@stuff) = split (/:/); $data{$a}{$b} = [ @stuff ]; }
    Whatever method you choose to put the stuff in there, you are going to have to get it out. The tricky part is figuring out how big your grid is, no? If each row can have holes, you are going to have to find all possible columns up front, like so:
    my %column_width; foreach my $row (keys %data) { foreach my $column (keys %{$data{$row}}) { my $data_length = length($data{$row}{$column}); if (!exists($column_width{$column}) { # Make sure the label fits $column_width{$column} = length($column); } if ($column_width{$column} < $data_length) { $column_width{$column} = $data_length; } } } # Order the columns accordingly. You may need a better sort. my @columns = sort keys %column_width; print join (" | ", sprintf ("%15s", ""), map { sprintf ("%*s", $column_width{$_}, $_) } @columns; ),"\n"; foreach my $row (sort keys %data) { my $continued; my $line = 0; do { $continued = 0; print join (" | ", # Only title the first line (0th) sprintf ("%15s", $line? '' : $row), map { $continued++ if (defined($data{$row}{$_}[$line+1] +)); sprintf ("%*s", $column_width{$_}, defined($data{$row}{$_}[$line])? $data{$row}{$_}[$line] : ''); } @columns ), "\n"; $line++; } while ($continued); }
    For simplicity, I have chosen 15 as an arbitrary width. It is trivial to calculate this, as has been done for each column.

    Testing and debugging, as they say, is left as an exercise for the reader.
Re: Changing the way data is formatted, a three year conundrum
by kappa (Chaplain) on May 07, 2002 at 09:10 UTC
A reply falls below the community's threshold of quality. You may see it by logging in.