in reply to What can I do to improve my code - I'm a beginner
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: What can I do to improve my code - I'm a beginner (Updated)
by Anonymous Monk on Aug 11, 2017 at 08:35 UTC |