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 #### ----- Original Message ----- From: "Andrea Holstein" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Friday, November 16, 2001 9:19 PM Subject: Re: sub-routine help needed. > "Merrychristmas!" wrote: > > > > 1. @array = qw ( hello world hello how are you ); > > 2. $match = 'HEllo'; > > 3. print "Your search for $match returns $subroutine $count > > records<br>\n"; > > > > $subroutine = &count; > > sub count { > > foreach $record (@array){ > > if (grep /$match/i, $record) { > > print $record; > > $count++; > > }; > > }; > > }; > > There's another problem in it. > Usually the grep function is used in the form > grep subroutine array. > > The way to get all records in the array matching your match is > > foreach my $record (grep {/$match/i} @array) { > print $record; > $count++; > } > print $count; > > Another way is quite more "beautiful": > > my @records = grep {/$match/i} @array; > print $_ foreach (@records); > print scalar(@records); # equals to $count > > > By the way: > $match = 'HEllo' > ^ > Didn't you mean Hello. ?! > ^ > > Best Wishes, > Andrea > _________________________________________________________ Do You Yahoo!? Get your free @yahoo.com address at http://mail.yahoo.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]