Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Minimisation of codes (if grep {})

by ravi45722 (Pilgrim)
on Oct 01, 2015 at 09:38 UTC ( [id://1143501]=perlquestion: print w/replies, xml ) Need Help??

ravi45722 has asked for the wisdom of the Perl Monks concerning the following question:

open (INTER,"$Intermediate_file") or print "cannot open file\n"; while (my $intermediate_line = <INTER>) { chomp $intermediate_line; my @intermediate_array = split(/\|/,$intermediate_line); if ($intermediate_array[12] eq "GSM") { $MO_userdep_error++ if grep { /^\Q$intermediate_array[11]\E$/ } + @subscriber_error; $MO_network_error++ if grep { /^\Q$intermediate_array[11]\E$/ } + @network_error; $MO_system_error++ if grep { /^\Q$intermediate_array[11]\E$/ } + @system_error; }
In this code $intermediate_array11 contains error. I dont know which type of error it is. So, I am checking in each array. But my problem even the error present in @subscriber_error its checking in network & system error. By this my program is getting too late. If the error present in first array I want to skip the remaining. Help me???

Replies are listed 'Best First'.
Re: Minimisation of codes (if grep {})
by Athanasius (Archbishop) on Oct 01, 2015 at 10:08 UTC

    Hello ravi45722,

    By this my program is getting too late.

    In addition to the if / elsif / ... / else construct suggested by Corion, you might gain a speedup by using List::Util::any in place of grep. From the documentation for List::Util:

    Many cases of using grep in a conditional can be written using any instead, as it can short-circuit after the first true result.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      Thanks for that. But by using  /^\Q$intermediate_array[11]\E$/ I can grep exact word. Is this List::Util::any checks for exact words or works like simple grep????

        Is this List::Util::any checks for exact words or works like simple grep????

        One of the many, many great things about Perl is the volume and quality of the documentation. It will stand you in excellent stead to become familiar with how to access it. If you have already read the relevant documentation but have not understood it then by all means describe which part is giving you the trouble so that the documentation might be made even clearer.

Re: Minimisation of codes (hash)
by tye (Sage) on Oct 01, 2015 at 12:22 UTC
    $error_count{ $error_type{ $intermediate_array[11] } }++;

    grep { /^\Q...\E$/ } @list is the slow way of saying grep { $_ eq ... } @list which is way slower than using a hash.

    - tye        

Re: Minimisation of codes (if grep {})
by Corion (Patriarch) on Oct 01, 2015 at 09:40 UTC

    Maybe you want to learn about the "else" keyword? perlsyn.

Re: Minimisation of codes (if grep {})
by Laurent_R (Canon) on Oct 01, 2015 at 11:16 UTC
    You'll need to change statement modifiers:
    do_something() if ...
    to standard conditionals:
    if ($some_condition) { do_something(); }
    to be able to use else statements as suggested by Corion:
    if ($intermediate_array[12] eq "GSM") { if (grep { /^\Q$intermediate_array[11]\E$/ } @subscriber_error +) { $MO_userdep_error++ ; } elsif (grep { /^\Q$intermediate_array[11]\E$/ } @network_erro +r) { $MO_network_error++; } else { $MO_system_error++ if grep { /^\Q$intermediate_array[11]\E +$/ } @system_error; } }
    As mentioned by Athanasius, replacing grep by the any function of List::Utils module is also likely to speed up your program.

    Also, testing for string equality might be slightly faster than a regex.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2024-04-18 00:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found