Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Hi Anon Monk!
I encourage you to sign up for an account. An account is free and helps at least me understand who is posting what...

I found your code hard to follow. I present an alternative below and will offer a few comments.

  • I have no comments about the command line and Get::Opts. My code starts with a list of files to process.
  • The use of array subscripts is rare in Perl. My code below does have one such use to index the translation of a number into a string representing the day of week.
    Your code has an inappropriate and I think an incorrect use. for(my $i=1; $i < scalar @files; $i++) I think $i=0 would be correct. But in any event, in Perl use a foreach iterator over an array.
  • Instead of complicated flags like if($firstline==1){}, call a subroutine to deal with the first line.
  • Then setup a loop to process the 12 line chunks. I show a very standard way to do this. process_one_minute() will be called repeatedly until there a no complete 12 line chunks left.
  • I am sure that there are some formatting and spacing issues with the printout.
  • But I hope that his helps..
  • Some Code:
    #!/usr/bin/perl use strict; use warnings; use Getopt::Long; use Date::Calc qw/Date_to_Time/; my @files = ('FiveSecondSamples.txt'); my @weekDay_text = qw(Sun Mon Tue Wed Thu Fri Sat); #### This is the Main Loop..., note that the main loop is "short" foreach my $in_file (@files) { open my $fh_in, '<', "$in_file" or die "unable to open $in_file $ +!"; open my $fh_out, '>', "$in_file"."_outfile.txt" or die "unable to open outfile for $in_file $!"; # deal with the special first line # my $current_time = process_header_line( $fh_in, $fh_out); # process the data in one minute chunks (12 samples per minute) # while ($current_time = process_one_minute($fh_in, $fh_out, $curren +t_time)){}; } #### #### The Main Loop is "over", now subroutines in the code... #### ## Process the input header line for start time and setup the output h +eader # sub process_header_line { my($fh_in, $fh_out) = @_; my $first_line = <$fh_in>; print $fh_out "#$first_line"; ### debug ## first input line as com +ment my ($year1,$month1,$day1,$hour1,$min1,$sec1, $year2,$month2,$day2,$hour2,$min2,$sec2,) = $first_line =~ /(\ +d+)/g; # with caveats my $start_time = Date_to_Time($year1,$month1,$day1,$hour1,$min1,$s +ec1); print $fh_out "Date\t\tTime\t\tDay\tmg\n"; # print output file +title line return $start_time; } ## Process each minute of data (12 samples) ## A block of data with less than 12 samples is not processed # sub process_one_minute { my ($fh_in, $fh_out, $current_time) = @_; my $total = 0; for (1..12) { my $in = <$fh_in>; return 0 unless defined $in; # No output if <12 samples in this + set. # This the "eof, "file empty" retu +rn, # no more complete sets of 12 samp +les my ($num) = split(',',$in); $total += $num; } my $avg = $total/12; $current_time += 60; # add 60 seconds to the "epoch time" my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime($current_time); printf $fh_out "%d-%02d-%02d\t%02d:%02d:%02d", $year+1900, $mon+1, $mday, $hour, $min, $sec; print $fh_out "\t",$weekDay_text[$wday]; print $fh_out "\t$avg\n"; return $current_time; }
    The Input:
    acceleration (mg) - 2013-10-09 10:00:00 - 2013-10-16 09:59:55 - sample +Rate = 5 seconds, imputed 46.3, 0 17.1, 0 30.1, 0 38.4, 0 97.1, 0 87.3, 0 84, 0 78.5, 0 67.9, 0 83.5, 0 155, 0 103.5, 0
    The Output:
    #acceleration (mg) - 2013-10-09 10:00:00 - 2013-10-16 09:59:55 - sampl +eRate = 5 seconds, imputed Date Time Day mg 2013-10-09 10:01:00 Wed 74.0583333333333

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

    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?

What's my password?
Create A New User
[marto]: this cpan day there should be a call to remove old crud :P
[marto]: as well as encourage active development

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2018-07-17 08:25 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (359 votes). Check out past polls.