> Thanks Cripps & Members,
> What should be the way to simplify the undermentioned working code as Part 1
> and part 2 are repetition.

Actually they aren't. Look at them and see.  :-)

It's late here. It might be late where you are too.
That makes things that are different look alike sometimes.

Neither is quite like what you had in your last msg.

But in general, putting something in a sub is one way
to eliminate repetition.  So once you get the sub you
want, just delete the  repetitious code in the calling
script.

> ## part 1 ##
> foreach $record (@array){
>         if (grep /$match/i, $record) {
>            print "$record<br>";
>         };
> };

This doesn't keep track of the count of matching "records".

It's also not a sub.

> ## part 2 ##
> sub count {
>         $count =0;
>         foreach $record (@array){
>              if (grep /$match/i, $record) {
>                    $count++;
>              };
>         };
>         print $count;
> };

This one is a sub.

It doesn't print any matches.

It does  keep a count, but doesn't return that value. IIRC your
last msg's sub did. This almost certainly doesn't do what
you want or expect.  Try and see.

from perldoc perlsub:

    The return value of the subroutine is the value of the
    last expression evaluated.  Alternatively, a return
    statement may be used to exit the subroutine, optionally
    specifying the returned value, which will be evaluated in
    the appropriate context (list, scalar, or void) depending
    on the context of the subroutine call.

The last expression here is your print statement, which
does have a return value.   My guess is that you want
the print statement in the calling code, as you had it
before.

-- 
 Free Dmitry Sklyarov: http://www.freesklyarov.org
 Repeal the DMCA: http://www.anti-dmca.org
 It's fair use or no use: http://www.nyfairuse.org


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to