Hello monks, I have a question/problem concerning a printf in an if-statement. In the following code I am going through a file with data in time. Some of this data is worthless which is represented by a file holding the start and endtimes of these worthless timespans in epoch seconds($gapsfile). For each day I want to calculate the number of gaps as well and print these to file. However for some reason the printf in the if-statement which checks if a next day begins ( if(! ($julday == $julday_old)) ) does not work. It does print to filehandle OUT1 before the if statement. The print statements, printing the same variables to standard output in the if-statement do function as well. I am confused.....?? thanks in advance!
my $error_cnt = 0; my $day_cnt = 0; my $month = 0; my $test; my $julday_old; open(OUT,">$resultsfile.corrected"); open(OUT1,">$resultsfile.gaps"); open(INP, $resultsfile); while (<INP>){ chomp($_); my $line = $_; my ($timestr,$timebin,$fish,$vel,$azi,$px,$py) = split(/\s+/,$line); my ($year,$julday,$hour,$min,$sec,$msec) = split(/\./,$timestr); my $timest = timegm_nocheck($sec,$min,$hour,$julday,$month,$year); my $timeend = $timest + 512; $julday_old = $julday if $. == 1; #$test = sprintf"%s %s\n",$julday_old,$day_cnt if $. != 1; #printf OUT1 "%s",$test; if(! ($julday == $julday_old)){ printf OUT1 "%s %s\n",$julday_old,$day_cnt; print "$julday_old "; print "$day_cnt\n"; $julday_old = $julday; $day_cnt = 0; } open(IN,"$gapsfile"); while (<IN>){ chomp($_); my ($sttime,$endtime) = split(/\s+/,$_); if ($sttime < $timest && $endtime > $timeend){ print "Timespan $timestr will be removed\n"; $day_cnt++; $error_cnt++; last; } elsif ($endtime > $timest+3.2 && $endtime < $timeend){ print "Timespan $timestr will be removed\n"; $day_cnt++; $error_cnt++; last; } elsif ($sttime+3.2 < $timeend && $sttime > $timest){ print "Timespan $timestr will be removed\n"; $day_cnt++; $error_cnt++; last; } else{ printf OUT "$line\n"; } } close(IN); } close(OUT); + close(OUT1); print "\n\nTotal number of timespans of 8m 32s removed is: $error_cnt\ +n"; close(INP);

In reply to printf in if-statement by Utrecht

Title:
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, details, 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, summary, 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.