in reply to Hash & Array

I thought this would be an easy one, until I tried it. If you want to learn perl here is a good example of lots of perl idioms, half documented, but perltidy-ed) code.
use strict; $_ = <DATA>; #get first line my @times = /(\S+\s+)/g; my @widths = map length, @times; $widths[-1] = -1; # get rid of last width s/\s//g for @times; # get rid of spaces my $food; my %hash; while ( my $line = <DATA> ) { my $newfood = substr $line, 0, $widths[0], ""; $newfood =~ s/\s//g; $food = $newfood if length $newfood; for ( 1 .. $#times ) { my $name = substr $line, 0, $widths[$_], ""; $name =~ s/\s//g; $hash{$food}{ $times[$_] } = [] unless defined $hash{$food}{ $times[$_] }; push @{ $hash{$food}{ $times[$_] } }, $name if length $name; } } shift @times; #get rid of location for my $food ( sort keys %hash ) { print "$food\n", "-" x length($food), "\n"; for my $time (@times) { print "$time ", join( " ", @{ $hash{$food}{$time} } ), "\n"; } print "\n"; } __DATA__ LoCATION 10:00 10:30 11:00 11:30 12:00 PiZZA SAM MARY LARRY ED LUSI RICK SUE DON SEAFOOD KIN KHAN DANNY MIKE YOUND TK MONK
outputs
PiZZA ----- 10:00 SAM LUSI 10:30 MARY RICK DON 11:00 LARRY 11:30 ED SUE 12:00 SEAFOOD ------- 10:00 KIN 10:30 KHAN MIKE 11:00 YOUND 11:30 TK 12:00 DANNY MONK

--

flounder

Replies are listed 'Best First'.
Re: Re: Hash & Array
by Anonymous Monk on Feb 25, 2004 at 03:36 UTC
    Nice one flounder . You are right not an easy one I tried doing it with a HOHash but not a nice one