John W. Krahn wrote:

$ echo "Hr 12
0001 2
0002 3
0003 1
Hr 13
0001 2
0002 3
0003 1
Hr 14
0001 2
0002 3
0003 1" | perl -e'

my ( @hours, %data );
while ( <> ) {
    push @hours, $1 if /^hr\s*(\d+)/i;
    $data{ $1 }{ $hours[ -1 ] } = $2 if /^(\d+)\s+(\d+)/;
    }

print join( "\t", "", @hours ), "\n";
for my $row ( sort { $a <=> $b } keys %data ) {
    print $row;
    for my $col ( sort { $a <=> $b } keys %{ $data{ $row } } ) {
        print "\t$data{$row}{$col}";
        }
    print "\n";
    }
'
12 13 14
0001 2 2 2
0002 3 3 3
0003 1 1 1

Probably better:

$ echo "Hr 12
0001 2
0002 3
0003 1
Hr 13
0001 2
0002 3
0003 1
Hr 14
0001 2
0002 3
0003 1" | perl -e'

my ( @hours, %data );
while ( <> ) {
    push @hours, $1 if /^hr\s*(\d+)/i;
    $data{ $1 }{ $hours[ -1 ] } = $2 if /^(\d+)\s+(\d+)/;
    }
print join( "\t", "", @hours ), "\n";
for my $row ( sort { $a <=> $b } keys %data ) {
    print $row;
    for my $col ( @hours ) {
        print "\t$data{$row}{$col}";
        }
    print "\n";
    }
'
        12      13      14
0001    2       2       2
0002    3       3       3
0003    1       1       1




John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction.                   -- Albert Einstein

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to