On Sat, Nov 17, 2001 at 05:48:44PM +0800, Leon wrote:
> First I would like to Thank ALL the Members for the help !
> Secondly I would like to Thank those that urge me to use "use strict" &
> "my".
>
> Unfortunately I think a lot of members have misunderstood my earlier-first
> question.
> My question is:- what must I do so that the output layout would look like
> this :-
>
> Your search for HEllo returns 2 records.
> hello
> hello
>
> ### Given the following #####
> @array = qw ( hello world hello how are you );
> $match = 'HEllo';
> #######################
>
> --- and this is what I've done but not to my satisfaction ------
> --- because the foreach-loop run twice and I was thinking --
> --- is there a way to come out with a better subroutine so --
> -- that I need not have to overwork the server to run double
> time ------------
>
> #!d:\perl\bin\perl.exe -w
> use CGI::Carp qw(fatalsToBrowser);
> use strict;
>
> print "content-type: text/html\n\n";
>
> my (@array , $match , $counter , $record );
> @array = qw ( hello world hello how are you );
> $match = 'HEllo';
>
> $counter = &count;
>
> print "Your search for $match returns $counter records.<br>\n";
>
> foreach $record (@array) {
> if (grep/$match/i,$record) {
> print "$record<br>\n";
> };
> };
>
>
> sub count {
> my ($record , $count);
> foreach $record (@array) {
> if (grep/$match/i,$record) {
> $count++;
> };
> };
> return $count; ## Thanks to Jos I . Boumans for this return thing.
> };
>
> ### end of script ####
Untested...
#!d:\perl\bin\perl.exe -w
use CGI::Carp qw(fatalsToBrowser);
use strict;
print "content-type: text/html\n\n";
my @array = qw ( hello world hello how are you );
my $match = 'HEllo';
my @matches = grep /$match/i, @array;
my $counter = @matches;
print "Your search for $match returns $counter records.<br>\n";
for my $record (@matches)
{
print "$record<br>\n";
};
--
Paul Johnson - [EMAIL PROTECTED]
http://www.pjcj.net
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]