Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Hello Anonymous Monk,

Some commends that could improve the speed of your script. Unfortunately I do not have the time to review it all and commend as many things I could suggest (sorry for that) but with a quick look:

Regarding your while loop. Since you are using a while loop (meaning that automatically) will read one line at a time you are assigning next line and repeating the same process for the first 12 lines, why not use also an if condition based on line number. Sample bellow:

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %HoA; while (<>) { chomp; if ( $. <= 12 ) { if (index($_, ',') != -1) { my @fields = split(/,/, ); push @{ $HoA{$fields[0]} }, @fields; $. = 0 if $. == 12; # reset line number } else { warn "Line could not be parsed: $_\n"; } } } continue { close ARGV if eof; } print Dumper \%HoA; __END__ $ perl test.pl test.csv $VAR1 = { 'Anand' => [ 'Anand', '1', '2', '3', '4', 'xyz', 'Anand', '2', '3', '4', '5', 'wer', 'Anand', '3', '4', '4', '4', 'ert', 'Anand', '2', '2', '2', '2', 'tre' ], 'seetha' => [ 'seetha', '1', '2', '3', '4', 'rew' ] };

The data that I used are coming from Re: Multiple values for a single key (Updated), but it should work out of the box for your case also.

__DATA__ Anand,1,2,3,4,xyz Anand,2,3,4,5,wer Anand,3,4,4,4,ert seetha,1,2,3,4,rew Anand,2,2,2,2,tre

By creating a HASHES OF ARRAYS you have the ability to extract the keys and values easier.

Update: Or if you prefer to reduce it by one line more and create HASHES OF ARRAYS and use as a key the line number (for easier data retrieval) you can do it like this. Sample bellow:

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %HoA; while (<>) { chomp; if ( $. <= 12 ) { if (index($_, ',') != -1) { push @{ $HoA{$.} }, split(/,/, ); $. = 0 if $. == 12; # reset line number } else { warn "Line could not be parsed: $_\n"; } } } continue { close ARGV if eof; } print Dumper \%HoA; __END__ $ perl test.pl test.csv $VAR1 = { '5' => [ 'Anand', '2', '2', '2', '2', 'tre' ], '1' => [ 'Anand', '1', '2', '3', '4', 'xyz' ], '3' => [ 'Anand', '3', '4', '4', '4', 'ert' ], '2' => [ 'Anand', '2', '3', '4', '5', 'wer' ], '4' => [ 'seetha', '1', '2', '3', '4', 'rew' ] };

Update2: You can reduce to minimum, just check if line contains comma (process) else skip.

my %HoA; while (<>) { chomp; if ( $. <= 12 ) { next unless ( index($_, ',') != -1 ); # or remove it also push @{ $HoA{$.} }, split(/,/, ); $. = 0 if $. == 12; # reset line number } } continue { close ARGV if eof; }

Update3: Even further:

my %HoA; while (<>) { chomp; next unless ( index($_, ',') != -1 ); # remove if not necessary push @{ $HoA{$.} }, split(/,/, ) if ( $. <= 12 ); $. = 0 if $. == 12; # reset line number } continue { close ARGV if eof; }

Update4: Line numbering reset, sorry just remembered you said you want to read every 12 lines a file with thousands of lines:

my %HoA; while (<>) { chomp; next unless ( index($_, ',') != -1 ); # remove if not necessary push @{ $HoA{$.} }, split(/,/, ) if ( $. <= 12 ); $. = 0 if $. == 12; # reset line number } continue { close ARGV if eof; }

Hope this helps, BR.

Seeking for Perl wisdom...on the process of learning...not there...yet!

In reply to Re: What can I do to improve my code - I'm a beginner (Updated) by thanos1983
in thread What can I do to improve my code - I'm a beginner by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    [zentara]: ... an unatural darkness descends on the realm
    [shmem]: eclipse? Java? omg...!
    [chacham]: The darkness here is quite natural. Sunset was 1.5 hours ago ...

    How do I use this? | Other CB clients
    Other Users?
    Others imbibing at the Monastery: (5)
    As of 2017-08-21 18:15 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Who is your favorite scientist and why?



























      Results (324 votes). Check out past polls.

      Notices?