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

Hello monks

I have the following data in this format:

HOUR*00:MODEL*123.12.100.11: U:1:23 L:1:2 D:1:- HOUR*00:MODEL*123.12.100.12: U:1:86 L:1:- D:1:24 HOUR*01:MODEL*11MT-AS5300: U:8:- L:8:10 D:8:32 U:9:2 L:9:5 D:9:- HOUR*02:MODEL*132.21.001.21: U:1:6 L:1:8 D:1:2
Any ideas how I can format it to reflect this
MODEL HOUR HOUR HOUR 0 1 2 123.12.100.11 U 1:23 L 1:2 D 1:- 123.12.100.12 U 1:86 L 1:- D 1:24 11MT-AS5300 U 8:- L 8:10 D 8:32 132.21.001.21 U 1:6 L 1:8 D 1:2
Any ideas for a data structure?

Replies are listed 'Best First'.
Re: Formatting questions
by tachyon (Chancellor) on Feb 07, 2003 at 00:47 UTC

    Here is how to chop it up into a data structure to print as you desire:

    use Data::Dumper; undef $/; my $data = <DATA>; my @data = split 'HOUR\*', $data; shift @data; my (%struct, @order); for my $bit (@data) { my ($model) = $bit =~ m/MODEL\*([^:]+)/; push @order, $model; my ($hour) = $bit =~ m/^(\d\d)/; my @u = $bit =~ m/^U:([^\n]+)/mg; my @l = $bit =~ m/^L:([^\n]+)/mg; my @d = $bit =~ m/^D:([^\n]+)/mg; $struct{$model} = { hour => $hour, u => \@u, l => \@l, d => \@d }; } print Dumper \%struct; for my $model( @order ) { print "$model\n"; print 'U', "\t" x ( 1 + $struct{$model}->{hour} ), $_, "\n" for @{ +$struct{$model}->{u}}; print 'L', "\t" x ( 1 + $struct{$model}->{hour} ), $_, "\n" for @{ +$struct{$model}->{l}}; print 'D', "\t" x ( 1 + $struct{$model}->{hour} ), $_, "\n" for @{ +$struct{$model}->{d}}; } __DATA__ HOUR*00:MODEL*123.12.100.11: U:1:23 L:1:2 D:1:- HOUR*00:MODEL*123.12.100.12: U:1:86 L:1:- D:1:24 HOUR*01:MODEL*11MT-AS5300: U:8:- L:8:10 D:8:32 U:9:2 L:9:5 D:9:- HOUR*02:MODEL*132.21.001.21: U:1:6 L:1:8 D:1:2

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

      Thanks tachyon,
      Thats exactly what I was looking for, I'll have to change some stuff, but this gives me a good foundation to start with.

      Thanks again

        Glad to help. You could use a counter to log the max hours in the munging loop (makes it easy to know the column width) and gild the lily using Spreadsheet::WriteExcel to make it really snazzy if you want to impress the PHB.

        cheers

        tachyon

        s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Formatting questions
by tall_man (Parson) on Feb 07, 2003 at 00:10 UTC
    It's not at all clear from your question if you need a "data structure" at all. You could simply read line by line, pattern-match the "HOUR" lines, and indent the U, L, and D lines to the hour column, perhaps using something like:
    my $spaces = $hour * 8; my $padding = ' ' x $spaces;
    It also appears you want to skip U/L/D blocks when there is no HOUR header. Is that correct?
      no, actually that was a typo
      It should look like this:
      MODEL HOUR HOUR HOUR 0 1 2 123.12.100.11 U 1:23 L 1:2 D 1:- 123.12.100.12 U 1:86 L 1:- D 1:24 11MT-AS5300 U 8:- U 9:2 L 8:10 L 9:5 D 8:32 D 9:- 132.21.001.21 U 1:6 L 1:8 D 1:2
        I think you should tell us what you are trying to accomplish rather than having us guess from examples.

        Are there sometimes more or less than three hours? Can there be many sets of U/L/D's or just one or two? Can one model have entries in several hour columns?