Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

scalar grep not binding hash slots

by princepawn (Parson)
on Nov 11, 2003 at 23:12 UTC ( [id://306409]=perlquestion: print w/replies, xml ) Need Help??

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),

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://306409]
Approved by Roger
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2024-04-24 23:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found