Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://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

    Title:
    Use:  <p> text here (a paragraph) </p>
    and:  <code> code here </code>
    to format your post; it's "PerlMonks-approved HTML":



    • Are you posting in the right place? Check out Where do I post X? to know for sure.
    • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
      <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
    • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
    • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found