Hello~
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!!) :-)
Brian