in reply to Re: without looping, check if $var is found in @list
in thread without looping, check if $var is found in @list

After experimenting with the code I seem to have weird results.

I am implementing your code into an $sth->fetch. My results SORT OF work. The thing that's odd is if the very first fetch'd row does not match, NOTHING matches.

However, if I remove any @choice but the first element, all the others work as expected. @choice is populated from a forum field where the user selects a few options via checkboxes.

while ($sth->fetch) { if( grep { $_ eq $name } @choices ) {
This behavoir is a little weird and I can't imagine what could be going wrong. Any ideas?

Replies are listed 'Best First'.
Re^3: without looping, check if $var is found in @list
by graff (Chancellor) on Dec 14, 2006 at 05:06 UTC
    It really is not clear what you are trying to do (and your extra reply doesn't help much either). Your description ("if the very first row does not match, NOTHING matches") is what I would expect, based on the snippet -- the data coming from the fetch would appear to have no bearing at all on whether your "@choices" array contains a match for the "$name" param.

    Based on the snippet you've presented, it looks like the same condition (with the same data, and therefore producing the same result) is being repeated on every iteration over the "while ()" loop.

    BTW, please consider signing up as a regular PerlMonks user (rather than Anonyous Monk), so you can get notifications of replies to your posts, and can update your posts when you want to (instead of posting more replies). It's harmless.

Re^3: without looping, check if $var is found in @list
by Anonymous Monk on Dec 14, 2006 at 02:37 UTC
    I don't think I was clear enough..

    When my DB query prints out it's always in a specific order. If the option in @choices doesn't match the very first row retrieved from my database, everything fails. However, if the first row DOES match something in @choices all the other ones that match the critera work and the ones that don't are discarded as I expect.

    It's just that the first row is being strange, if it doesn't match @choices nothing will.