princepawn has asked for the wisdom of the Perl Monks concerning the following question:

Can someone tell me why rules and rules2 are not being bound by the use of scalar grep? Any help is appreciated.

CODE

{ my @DRIVER = Alzabo::Driver->available; my @RULE = Alzabo::RDBMSRules->available; my $sg = (scalar grep { $p{rdbms} eq $_ } Alzabo::RDBMSRules->av +ailable); warn Data::Dumper->Dump([\@DRIVER, \@RULE, $sg], [qw(DRIVER RULE + sg)] ); my %has = ( driver => grep { $p{rdbms} eq $_ } Alzabo::Driver->available, rules => (scalar grep { $p{rdbms} eq $_ } Alzabo::RDBMSRules->av +ailable) ? 'yes' : 'no', rules2 => (scalar grep { $p{rdbms} eq $_ } Alzabo::RDBMSRules->av +ailable) ); use Data::Dumper; die Dumper(\%has);

OUTPUT

~/hacks/gerbold/scripts $ perl s.pl $DRIVER = [ 'Gerbold', 'MySQL', 'PostgreSQL' ]; $RULE = [ 'MySQL', 'PostgreSQL' ]; $sg = 0; $VAR1 = { 'driver' => 'Gerbold' }; ~/hacks/gerbold/scripts $
Carter's compass: I know I'm on the right track when by deleting something, I'm adding functionality

Replies are listed 'Best First'.
Re: scalar grep not binding hash slots
by japhy (Canon) on Nov 11, 2003 at 23:56 UTC
    You've got TWO problems. You need to limit the arguments to the grep(), and you need to use an array reference, not a list, as the value of the 'driver' key in the hash. In fact, by doing the second one, you do the first.
    my %hash = ( driver => [ grep $p{rdbms} eq $_, Alzabo::Driver->available ], rules => scalar(grep $p{rdbms} eq $_, Alzabo::RDBMSRules->available +) ? 'yes' : 'no', rules2 => scalar(grep $p{rdbms} eq $_, Alzabo::RDBMSRules->available +), );

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a job (NYC-area)
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Re: scalar grep not binding hash slots
by Roger (Parson) on Nov 11, 2003 at 23:26 UTC
    Problem is not with your $rules nor $rules2, it's with your driver=> line.

    Change your code to this and your code with work -

    my %has = ( # japhy was right, use [ ] to return reference to array driver => [grep { $p{rdbms} eq $_ } Alzabo::Driver->available], rules => (scalar grep { $p{rdbms} eq $_ } Alzabo::RDBMSRules->av +ailable) ? 'yes' : 'no', rules2 => (scalar grep { $p{rdbms} eq $_ } Alzabo::RDBMSRules->av +ailable) );
Re: scalar grep not binding hash slots
by ysth (Canon) on Nov 11, 2003 at 23:30 UTC
    The rules and rules2 lines are part of the list input to the first grep. Try: (driver => grep { $p{rdbms} eq $_ } Alzabo::Driver->available),