On 8/28/07, Chris <[EMAIL PROTECTED]> wrote:
> Yet another problem from Intermediate Perl, I've been given a log
> file.  Here is a sample of its contents:
>
> Gilligan: 1 coconut
> Skipper: 3 coconuts
> Gilligan: 1 banana
> Professor: 3 coconuts
> MaryAnn: 2 papayas
> Thurston: 2 mangoes
>
> I am supposed to read the log file and output its contents into a
> series of files called gilligan.info, maryann.info, etc.  In the end I
> should I have a .info file for each character.  In the file will be
> information pertaining to the character.  For example, gilligan.info
> will have
> Gilligan: 1 coconut
> Gilligan: 1 banana
>
> According to the book, I'm supposed to process the file in one pass
> and then output the files in parallel.  Here is what I have written so
> far:
>
> #!/usr/bin/perl -w
> use strict;
> use IO::File;
>
> my $castaway;
> my %total_fruit;
>
> while (<>) {
>
>         if (/(^\S+):/) {
>                 $castaway = $1;
>                 $castaway =~ tr/A-Z/a-z/;
>                 my $castaway_fh = $castaway.".info";
>                 $total_fruit{$castaway} = IO::File->new($castaway_fh);
>                 print {$total_fruit{$castaway}} $_;
>         }
>
>
> }
>
>
> This script generated the following error message:
> Can't use an undefined value as a symbol reference at read_log.pl line
> 15, <> line 1.
>
> What does the message mean?  I must admit I am confused by book's
> instructions.  I don't understand what the book means when it wants me
> to "write all ouput files in parallel".

The error message means that you have an undef value being used as if
it were a reference on line 15.  This usually means something has gone
wrong above this line and your program has failed to set a variable.
Many functions in Perl will return an undef value if they don't
succeed.  It is your job to catch these errors at runtime.

You are opening a file without checking to see if you were successful
(you aren't, see above).   You are also trying to opening a file for
reading when you want to write to it.  Go back and reread about the
arguments to IO::File->new().

By "write all ouput files in parallel" the book means that you are
storing the file handles in a hash and writing to correct one on each
iteration of the loop as opposed to writing to the files in a serial
fashion (first all of the Gilligan writes, then all of the Skipper
writes, etc.).

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to