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


In reply to Re: Hash & Array by flounder99
in thread Hash & Array by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.