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

hi Monks , I am new to perl and working on this practice which is a littel challengin to me : my script generate the following input file :
INPUT1 -------- 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
which contains the location and who is working at what time on that location . I am trying to read it and print output like this :
output -------- PIZZA ----- 10:00 SAM LUSI 10:30 MARY RICK DON 11:00 LARRY 11:30 ED SUE 12:00 SEAFOOD ------- 10:00 KIM 10:30 KHAN MIKE 11:00 YOUNG 11:30 TK 12:00 DANNY MONK
Any idea of the best way in doing this ? not sure weather arrays or hash is better? Thanks for advice and help

Replies are listed 'Best First'.
Re: Hash & Array
by davido (Cardinal) on Feb 24, 2004 at 05:57 UTC
    One possibility:

    Use a RE to determine whether you're dealing with PIZZA or SEAFOOD. Use unpack to generate a list of names. And populate the names into a hash of lists, where the hash keys are times of day, and the value of each key is an anonymous array filled with names.

    The RE is probably unnecessary though if you just use a little logic to look at the first element of what unpack returns to set a sticky flag for PIZZA or SEAFOOD.

    Update: Ultimately the structure might look something like this:

    %hash = ( 'PIZZA' => { '10:00' => [ 'Dave', 'Dan'], '10:15' => [ 'Marge, 'Homer ] }, 'SEAFOOD'=>{ '10:00' => [ 'Pete', 'Jose' ], '10:30' => [ 'Kim', 'Aileen' ] } );


    Dave

Re: Hash & Array
by arden (Curate) on Feb 24, 2004 at 06:33 UTC
    I would probably make a hash of hashes, with the first key being the location, the second key being the time, and the value being the person/people working. Ultimately, I would say that it's up to you, your professor, and what lesson you're working on, as this smells very strongly of homework. ;^)

    If you are having trouble with it, show us your code and what errors you're getting, and I'm sure we'd be happy to help you learn. . .

    - - arden.

Re: Hash & Array
by flounder99 (Friar) on Feb 24, 2004 at 21:13 UTC
    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

      Nice one flounder . You are right not an easy one I tried doing it with a HOHash but not a nice one
Re: Hash & Array
by fuzzyping (Chaplain) on Feb 24, 2004 at 20:03 UTC
    Personally, I see it as a HoHoA (Hash of Hash of Array). Use the food as the initial key, with the time being the next key, and the attendees in the array. The data could be something like this:
    my %schedule = ( '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' ], }, );
    It's a little advanced for a beginner, but the data is definitely there and accessible. HTH.

    -fp