Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
Think about Loose Coupling
 
PerlMonks  

grep not working as expected

by davidj (Priest)
on Oct 17, 2006 at 22:14 UTC ( #578911=perlquestion: print w/ replies, xml ) Need Help??
davidj has asked for the wisdom of the Perl Monks concerning the following question:

Fellow monks,
I have been scratching my head over this trying to figure out what I am doing wrong. For the life of me, I am not seeing it. I am using grep to test the value of a query paramater. The value IS in the list, but grep is not finding it. I hope I have included all the relevant information below.

The form part looks like this:
<select name="report_type" > <option value="case_id">case_id</option> <option value="hosp_alias">hosp_alias</option> <option selected="selected" value="sender">sender</option> <option value="receiver">receiver</option> <option value="date">date</option> </select>
After selecting any of the option (in this case sender), I do the following in the cgi script:
print "param=" . $q->param('report_type') . "<BR>"; if( grep { /$q->param('report_type')/ } (qw/case_id hosp_alias sender +receiver/) ) { print "we are getting the list<BR>"; } else { print "we are NOT getting the list<BR>"; }
The output I get is this:
param=sender we are NOT getting the list
At this point I am not modifying in any way the query parameters after they are sent to the script.
If I understand grep correctly, I should be getting:
param=sender we are getting the list
If I take this part and put it in a simple script, I works correctly:
#!/usr/bin/perl $p = 'sender'; if( grep { /$p/ } (qw/case_id hosp_alias sender receiver/) ) { print "getting the list\n"; } else { print "not getting the list\n"; }
output:
getting the list
Why am I not getting what is expected? The value is verifiably in the list. The grep syntax is correct.

As always your assistance is much appreciated.
davidj

Comment on grep not working as expected
Select or Download Code
Re: grep not working as expected
by imp (Priest) on Oct 17, 2006 at 22:31 UTC
    This isn't doing what you think:
    grep { /$q->param('report_type')/ } (qw/case_id hosp_alias sender rece +iver/)
    Regular expressions are interpolated, but not evaluated.

    What you want instead is:

    grep { $q->param('report_type') eq $_ } (qw/case_id hosp_alias sender +receiver/)
    Or:
    my $report_type = $q->param('report_type'); grep { $report_type eq $_ } (qw/case_id hosp_alias sender receiver/)
    And to avoid checking a long list you can get only the first match by using List::Util as follows:
    use List::Util qw( first ); if ( first { $report_type eq $_ } (qw/case_id hosp_alias sender receiv +er/)) { }
Re: grep not working as expected
by chargrill (Parson) on Oct 17, 2006 at 22:31 UTC

    Does it still not work if you do the following:

    my $param = $q->param('report_type'); print "param=" . $param . "<BR>"; if( grep { /$param/ } (qw/case_id hosp_alias sender receiver/) ) { print "we are getting the list<BR>"; } else { print "we are NOT getting the list<BR>"; }

    If it still doesn't work, then something is wrong somewhere else. If this does work, then perhaps the answer is tied to Fletch's response to the post just before yours, Re: Strange behaviour of m and $_ - in that (quoting Fletch) "Subroutine invoations don't interpolate in double quoted strings (of which m// is a special kind.)"



    --chargrill
    s**lil*; $*=join'',sort split q**; s;.*;grr; &&s+(.(.)).+$2$1+; $; = qq-$_-;s,.*,ahc,;$,.=chop for split q,,,reverse;print for($,,$;,$*,$/)
Re: grep not working as expected
by ammon (Sexton) on Oct 17, 2006 at 22:33 UTC
    I expect perl is just not DWIM-ing your method call in the regex:

    /$q->param('report_type')/

    I think perl's regex patterns are similar to a qq() string in regards to interpolation (i.e. you can't interpolate function calls), but I can't immediately find any documentation to back up that assertion.

Re: grep not working as expected
by GrandFather (Cardinal) on Oct 17, 2006 at 22:34 UTC

    Have you a newline or other white space following the parameter string?


    DWIM is Perl's answer to Gödel
Re: grep not working as expected
by davidj (Priest) on Oct 17, 2006 at 22:41 UTC
    Jeez, I am such a dork!! I am laughing at myself as I write this. I'm doing all this $q->param() stuff, and all the while I'm thinking, "hey, its a hash reference". I much appreciate you all pointing out the obvious to me.

    I tell ya, I need to get some sleep.
    davidj

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2014-04-19 20:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (483 votes), past polls