Brian Volk wrote:
>
> I have a couple of questions from chapter 4 in the Alpaca book.
>
> Everything is working fine but I keep getting "I don't understand
> blue_shirt" whether I create the anonymous array w/
>
> $provisions{$person} = [] unless exists $provisions{$person};
>
> Or if I comment out the above line and allow autovivification to create
> it. Could someone pls explain what I am not understanding?
>
> Below is the exact code from the Autovivification section... and sample
> data.
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> my %provisions;
> my $person;
>
> while (<>) {
>
> if (/^(\S.*)/) { # a persons name (no leading white spaces)
> $person = $1;
> $provisions{$person} = [] unless exists $provisions{$person};
> }elsif (/^s+(\S.*)/) { # a provision
> die "No person yet!" unless defined $person;
> push @{ $provisions{$person} }, $1;
> } else {
> die "I don't understand: $_";
> }
>
> }
>
> ___DATA___
>
> The Skipper
> blue_shirt
> hat
> jacket
> perserver
> sunscreen
> Professor
> sunscreen
> water_bottle
> slide_rule
> Gilligan
> red_shirt
> hat
> lucky_socks
> water_bottle
>
>
> Thank you! (Great book BTW!!) :-)
Hi Brian.
The reason your program is failing is that you don't have a backslash before the
's' in the elsif condition. The pattern is looking for a record that starts with
an s and doesn't find one - it's nothing to do with the autovivication of the
hash element. It should read:
elsif (/^\s+(\S.*)/)
By the way, the __DATA__construct is a valid Perl one, except that it has two
underscores before and after instead of your three. I at first thought there was
a problem that you were reading using <> instead of <DATA> until I realised that
you meant it just as a caption for the data in your message.
I hope this helps.
Rob
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>