Home > Archive > PERL CGI Beginners > September 2005 > Exact matching using GREP.
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Exact matching using GREP.
|
|
|
| my @present = ('perl', 'perl/chat', 'php', 'php/forums', 'php/counters', 'perl/search/scripts', 'php');
# Getting Results for mySQL query in hashref.
while (my $row = $sth->fetchrow_hashref)
{
if (grep /$row->{CAT_TITLE}/, @present) {
#matching title with @present elements
print "$row->{CAT_TITLE}";
}
Question is how to do EXACT matching using GREP? because the above code prints every element from array for 'php' if the $row->{CAT_TITLE} is 'php' it prints php/counters, php/forums and every element containing php.
How I am supposed to do Exact matching that if $row->{CAT_TITLE} is 'perl/chat' it should pick only 'perl/chat' from @present.
I saw the Unix man pages and did other searches and found '-x' switch to use for exact matching with GREP and '-e' switch for pattern matching but dont' know how to incorporate it within my code.
TIA,
Sara
| |
|
| No, it's not working, probably you didnt' get my question.
Anyways, thanks for a prompt reply.
Sara.
----- Original Message -----
From: "Ovid" <publiustemp-beginnerscgi2@yahoo.com>
To: <beginners-cgi@perl.org>
Sent: Friday, September 09, 2005 12:01 AM
Subject: Re: Exact matching using GREP.
> --- Sara <sara.samsara@gmail.com> wrote:
>
>
> Assuming I understood your question correctly:
>
> if (grep { $_ eq $row->{CAT_TITLE} } @present) {
> # do something
> }
>
> Cheers,
> Ovid
>
> --
> If this message is a response to a question on a mailing list, please send
> follow up questions to the list.
>
> Web Programming with Perl -- http://users.easystreet.com/ovid/cgi_course/
>
> --
> To unsubscribe, e-mail: beginners-cgi-unsubscribe@perl.org
> For additional commands, e-mail: beginners-cgi-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
| |
|
| --- Sara <sara.samsara@gmail.com> wrote:
> while (my $row = $sth->fetchrow_hashref)
> {
> if (grep /$row->{CAT_TITLE}/, @present) {
> #matching title with @present elements
> print "$row->{CAT_TITLE}";
> }
>
> Question is how to do EXACT matching using GREP? because the above
> code prints every element from array for 'php' if the
> $row->{CAT_TITLE} is 'php' it prints php/counters, php/forums and
> every element containing php.
Assuming I understood your question correctly:
if (grep { $_ eq $row->{CAT_TITLE} } @present) {
# do something
}
Cheers,
Ovid
--
If this message is a response to a question on a mailing list, please send
follow up questions to the list.
Web Programming with Perl -- http://users.easystreet.com/ovid/cgi_course/
| |
| Wiggins d'Anconia 2005-09-08, 6:55 pm |
| Please bottom post....
Sara wrote:
> No, it's not working, probably you didnt' get my question.
How is it not working now? What Ovid sent is exactly what I would have
answered so you probably need to provide more information. You mention
man pages and switches to grep, there are two greps here, 1) the command
line program used for searching files which is where your -x, etc. come
in and 2) 'grep' the function which is a Perl built-in. For the docs for
it, you need to check:
perldoc -f grep
They are very different things.
http://danconia.org
[color=darkred]
>
> Anyways, thanks for a prompt reply.
>
> Sara.
>
> ----- Original Message ----- From: "Ovid"
> <publiustemp-beginnerscgi2@yahoo.com>
> To: <beginners-cgi@perl.org>
> Sent: Friday, September 09, 2005 12:01 AM
> Subject: Re: Exact matching using GREP.
>
>
| |
| John W. Krahn 2005-09-09, 7:55 am |
| Sara wrote:
>
> ----- Original Message ----- From: "Ovid"
>
>
> No, it's not working, probably you didnt' get my question.
That's the way I read it and that is how you do it in Perl. It looks like you
think that Perl's grep is the same as the grep program you use on the command
line but it is not.
John
--
use Perl;
program
fulfillment
|
|
|
|
|