Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Inserting a header into outfile -- a long one

by Discipulus (Canon)
on Mar 14, 2017 at 21:18 UTC ( [id://1184609]=note: print w/replies, xml ) Need Help??


in reply to Inserting a header into outfile

hello jeretjordan and welcome to the monastery and to wonderful world of Perl!

We are here in the aim to help you, if this is possible. AnonymousMonk in his first replay is almost right in every point; I think you too agree.

You have probably done a big work to end up with this 400+ lines of code. Or you have received this code from an ancient hard disk IDE and you were tasked to modify it to add headers. No problem! If you are here to learn coding good Perl, at PerlMonks you'll find very good mentors.

If this is your code it is better to rethink the whole flow of the program. More on this later.

If you just need to modify it, is nastier: you must fully understand all the actual program structure and all possible bogus parts (and it seems full of them!) to spot where and how to do the modification: the e) point of the above mentioned Anoymous's post fit well enought.

If this is your code your are learning from very old codebases. Modern Perl is a lot more clean and concise and powerful and safer: absence of use strict; use warnings is guilty for the fact that they constrain you into safer lanes. An example? your program is full of scope errors. my is a duty when you use strict; but prevent you to declare many times the same var in the same scope (see your my $csv .. ).

You must learn from better and more idiomatic Perl: PerlMonks is a good place, full of good code by wise monks (be carefullwith mine anyway ;=). To read the free ModernPerl book is a must (it has a good chapter about the scope of variables).

Applied to your code a good sense review can spot many incorrect behaviours: when you cut & past code inside your own program, you loose. You MUST reuse code but abstracting it in subroutines. You have many times my $csv = Text::CSV_XS->new... open my $fh,...  while (my $row.... all this part is a good candidate for a sub. You can answer that each file need a different processing: the idiomatic dispatch table can become handy: a process_file sub take care of opening the file and other things then pass the filehadle by reference to different specialized sub to handle different file content. It is just an example.

Choose meninigfull names for your vars, avoid label like line: while.. , declare variable when you need them and in the correct scope, use strict; use warnings and also remeber to use strict; use warnings , abstract as much as possible, think about your data and datastructure well and early, discover right module to use and exploit them as much as possible, borrow from expert coders..

A possible approch must be similar to the following pseudocode:

# pseodocode !!! use strict; use warnings; use Text::CSV_XS; # declare early used vars and others my $TOLERANCE = 0.000001; # you can also use constants in Perl.. my %final_datastructure; #verify @ARGV filling @files my @files .. # define a lookup table for different files my %lookup_table{ type_a => \&proc_file_a, type_b => \&proc_file_b, ... } foreach my $file (@files){ # maybe check readbility? or construct the full path.. process_file ($file); } # output the data out_data(\%final_datastructure); # the general sub sub process_file{ my $fname = shift; my $icnt = 0; open my $fh, "<:encoding(utf8)", $fname or die "cannot Open $fname : + $!"; # pass the filehandle to specialized subroutines if ($fname eq 'type_a.csv'){ # pass the filehandle by reference proc_file_a(\$fh); } } # the specialized sub sub proc_file_a{ my $fh = shift; my $csv = Text::CSV_XS->new ({ binary => 1 }) or die "Cannot use CSV: ".Text::CSV_XS->error_diag (); while (my $row = $csv->getline ($fh)){ # fill in the destination datastructure.. ... # sub to output the data sub out_data{ ...

HtH

L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2024-04-25 08:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found