Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

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

by Anonymous Monk
on Aug 12, 2017 at 17:54 UTC ( #1197315=note: print w/replies, xml ) Need Help??

in reply to What can I do to improve my code - I'm a beginner

Some other comments, suggestions:

- There is this section where you read in the list of input files:

my @files = undef; #list of files to run script on while(my $line = <INPUT>){ chomp($line); push @files, $line; }

The @files = undef part was addressed already, but there is one more typical beginner pattern here. You read the file line by line then push every line to your array. This is inefficient and unnecessary. The <> operator in list context will read the entire file into the array for you, and you can use chomp on the array to remove newlines from every line. So a simple

my @files = <INPUT>; chomp @files;

is enough.

- I bet your script spends most of its time in the Add_Delta_DHMS function. Because you keep your dates in their complicated, human readable formats, you have to do complicated date-processing arithmetic every time you want to add 5 seconds to them. (It was the right call to use a module like Date::Calc instead of rolling your own buggy time increment code, but you pay the price for it: it's slow.) It would be better to convert the date/time you read from the first line of the file to a Unix timestamp with Time::Local, so that incrementing the rolling timestamp simply become $t += 5;, then use strftime or localtime when you print the date.

- Those chomps in the inner loop are unnecessary. If you really want to pare it down, and you are sure about your input format, you can even do away with the splits. If you have a string like "45.6, foo", and force it into scalar context (e.g. with an operator like +), Perl will take the 45.6 and ignore the rest. So you can even do something like

my $avg = <IN> + <IN> + <IN> + <IN> + <IN> + <IN> + <IN> + <IN> + <IN> + <IN> + <IN> + <IN>; $avg /= 12;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1197315]
[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 making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2017-08-21 18:17 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (324 votes). Check out past polls.