Reputation: 109
Newbie to perl. I am trying to grep from the values of a hash array. Can someone explain why I get uninitialized value error when i try
#!/usr/bin/perl
use strict;
use warnings;
my %families = (Flintstone => [ qw(Pebbles) ],
Simpson => [ qw(Bart Lisa Maggie) ],
Keaton => [ qw(Alex Mallory Jennifer Andy) ]);
my $user = 'Mary';
foreach my $name (keys %families)
{
print "$name has @{$families{$name}} \n";
if (grep /$user/,@{families{$name}})
{
print "User $user found \n"
}
else
{
print "User $user not found";
}
}
UPDATE: Thank you. I fixed $name. However grep doesnt seem to work for me. i.e if I change $user to Bart I still get User Bart not found.
Upvotes: 1
Views: 164
Reputation: 4104
You'll notice you get the error on line 13 but not on line 11? The missing $ may be important.
Upvotes: 1
Reputation: 67910
Did you mean
@{$families{$name}}
Instead of
@{families{name}}
Perhaps?
Upvotes: 2