Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: What can I do to improve my code - I'm a beginner (Updated)

by thanos1983 (Priest)
on Aug 10, 2017 at 10:39 UTC ( #1197149=note: print w/replies, xml ) Need Help??


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.

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

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
    Thanks for your reply! As I say I'm a beginner so this took quite a while to digest but now I've re-read it I'm starting to understand what you are saying. Thank you for your help!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1197149]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2017-10-23 21:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My fridge is mostly full of:

















    Results (285 votes). Check out past polls.

    Notices?