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

I have several elements of this things such as below:
###Data Transfer### $focus_count = 0; $random_25_count = 0; $random_50_count = 0; $cat_tes +t_count= 0; my %newhash=map{$_=>1} @data_trasfer_list; my @data_transfer_count=grep ($newhash{ (m/(\w+)/)[0] }, @newlist); my %newhash=map{$_=>1} @data_transfer_rt_25; my @data_tranfer_rt_25_count=grep ($newhash{ (m/(\w+)/)[0] }, @newlist +); $random_50_count = $#mpg_rt_50_count + 1; $random_25_count = $#data_transfer_rt_25_count + 1; $focus_total += $focus_count; $random_25_total += $random_25_count; $random_50_total += $random_50_count; $cat_test_count = $focus_count + $random_25_count * 25 + $random_50_co +unt *50; $key = "DATA_TRANSFER"; $~ = "LIST"; write; ###DEVSLP### $focus_count = 0; $random_25_count = 0; $random_50_count = 0; $cat_tes +t_count= 0; my %newhash=map{$_=>1} @devslp_list; my @devslp_count=grep ($newhash{ (m/(\w+)/)[0] }, @newlist); $focus_count = $#devslp_count + 1; my %newhash=map{$_=>1} @devslp_rt_25; my @devslp_rt_25count=grep ($newhash{ (m/(\w+)/)[0] }, @newlist); $random_25_count = $#devslp_rt_25_count + 1; my %newhash=map{$_=>1} @devslp_rt_50; my @devslp_rt_50_count=grep $newhash{ (m/(\w+)/)[0] }, @newlist; $random_50_count = $#devslp_rt_50_count + 1; $focus_total += $focus_count; $random_25_total += $random_25_count; $random_50_total += $random_50_count; $cat_test_count = $focus_count + $random_25_count * 25 + $random_50_co +unt *50; $key= "DEVSLP"; $~ = "LIST"; write; ###MPG### $focus_count = 0; $random_25_count = 0; $random_50_count = 0; $cat_tes +t_count= 0; my %newhash=map{$_=>1} @mpg_list; my @mpg_count=grep ($newhash{ (m/(\w+)/)[0] }, @newlist); my %newhash=map{$_=>1} @mpg_rt_50; my @mpg_rt_50_count=grep ($newhash{ (m/(\w+)/)[0] }, @newlist); $random_50_count = $#mpg_rt_50_count + 1; $focus_count = $#mpg_count + 1; $focus_total += $focus_count; $random_25_total += $random_25_count; $random_50_total += $random_50_count; $cat_test_count = $focus_count + $random_25_count * 25 + $random_50_co +unt *50; $key= "MPG"; $~ = "LIST"; write;
This is just part of my code. I got no idea on how to reduce the size of this lengthy code. Could anyone pls kindly give some suggestions and ideas. thx you very much

Replies are listed 'Best First'.
Re: How to reduce this lengthy code?
by AnomalousMonk (Archbishop) on Jul 03, 2012 at 06:59 UTC
    my @data_tranfer_rt_25_count=grep ($newhash{ (m/(\w+)/)[0] }, @newlist +); ... $random_25_count = $#data_transfer_rt_25_count + 1;

    The array  @data_tranfer_rt_25_count and other, similar arrays seem to be used only to hold the elements produced by a  grep so that the number of these elements can be determined later. This number can be determined directly:

    >perl -wMstrict -le "my @ra = qw(a b c d e f); my $n = grep /[bcd]/, @ra; print qq{b or c or d: $n}; " b or c or d: 3

    In more general terms, the three blocks of code you show seem to be doing rather similar things, but with different arrays.
    .oO(Thinks: Function!)

    Those are some pretty dense wadges of code there, pilgrim. Some whitespace/blank lines would be very much welcomed by these tired old eyes, and would help in sniffing out common code factors.

Re: How to reduce this lengthy code?
by pvaldes (Chaplain) on Jul 03, 2012 at 09:34 UTC

    First of all, this is an error probably

    @data_tranfer_rt_25_count ... $#data_transfer_rt_25_count

    note the extra S in tranSfer

    Second, this lines:

    my %newhash=map{$_=>1} @data_trasfer_list;

    can be replaced by:

    my %newhash; @newhash{@data_transfer_list} = (1) x scalar @data_transfer_list;

    Third. What's in @newlist?

Re: How to reduce this lengthy code?
by Anonymous Monk on Jul 03, 2012 at 06:51 UTC

    This is just part of my code. I got no idea on how to reduce the size of this lengthy code. Could anyone pls kindly give some suggestions and ideas. thx you very much

    A cynic might say hire a programmer :)

    Why do you want to reduce the size of this lengthy code? What bugs you about the code?

    The more things you can list, the easier it will be to make recommendations

Re: How to reduce this lengthy code?
by Steve_BZ (Chaplain) on Jul 03, 2012 at 09:24 UTC

    Hi HongPing,

    How do you know it's lengthy, maybe it's not long enough?

    It's nice that you have named variables, but your comments are sparse, your code is not SO nice that everyone will immediately know what it should do. I don't see any indentation or loops. If you want people's help, it is important to show maintainable code, even for yourself when you come back to fix something in a year's time, comments will help you. Some people write code that is just perfect and intuitive, but for the rest of us: we need comments.

    Good luck

    Steve.

Re: How to reduce this lengthy code?
by mrguy123 (Hermit) on Jul 04, 2012 at 15:03 UTC
    Hi there
    Like mentioned above, the trick is not to make your code shorter, but to make it readable. Usually code that is lengthy but well written is much easier to read than code that is short and confusing.
    Somehing that helped me to make my code more readable is Damian Conway's book Best Perl Practices
    It requires a bit of work, and not all of the tips he gives are the way to go, but overall it can really improve your code quality


    Hope this helps
    Mister Guy


    About half of the world's greatest inventions were invented by single men trying to impress women. The other half were invented by married men looking for an excuse to get out of the house
Re: How to reduce this lengthy code?
by Anonymous Monk on Jul 03, 2012 at 14:08 UTC
    Start by making it readable. Add white space and comments.