user1719051
user1719051

Reputation: 109

grep error in perl

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

Answers (2)

tjd
tjd

Reputation: 4104

You'll notice you get the error on line 13 but not on line 11? The missing $ may be important.

Upvotes: 1

TLP
TLP

Reputation: 67910

Did you mean

@{$families{$name}}

Instead of

@{families{name}}

Perhaps?

Upvotes: 2

Related Questions