I added a few comments to your script. I don't exactly know what outcome you want, but I do spot a few things that are most likely errors.

#!/usr/bin/perl -w use strict; use locale; use warnings; use diagnostics; use utf8; binmode(STDIN, ":encoding(utf8)"); # prepend the colon to "encoding" +. binmode(STDOUT, ":encoding(utf8)"); # prepend the colon to "encoding" +. binmode(STDERR, ":encoding(utf8)"); # prepend the colon to "encoding" +. my $data_file = "data.txt"; my ($start, $end); # Warning: this works, but DATA is special. Better to use a different + name. open (DATA, '<:utf8', $data_file) || die "Couldn't open $data_file : $ +!\n"; # Consider using IO::Prompt::Tiny, or IO::Prompt::Hooked. print "Start time (format : yyyy.mm.dd.hh):\n"; chomp (my $start_input = <STDIN>); print "End time (format : yyyy.mm.dd.hh):\n"; chomp (my $end_input = <STDIN>); # Questionable: Are you really intending to pass any input that contai +ns four # contiguous digits? Maybe so, but seems like an inadequate validatio +n. if ($start_input =~ /\d\d\d\d/ && $end_input =~ /\d\d\d\d/){ while (<DATA>){ # Even more questionable: None of your suggested input formats wou +ld match. # Did you mean $start_input =~ /\d{6}/ ? When would the input eve +r have # six contiguous digits? if (/$start_input\d\d\d\d\d\d/ .. /$end_input\d\d\d\d\d\d/){ if ($_ =~ /(.*)\t(.*)\t(.*)/){ print "Date = $1" . "\t" . "Temp_moy_DegCs = $2" . "\t" . "HR +_moy_s = $3\n"; } } } } if ($start_input =~ /\d\d\d\d\.\d\d/ && $end_input =~ /\d\d\d\d\.\d\d/ +){ # Maybe you intended to do this immediately after receiving input. +That would # fix the second "if", but would break the fourth "if" conditional. $start_input =~ tr/\.//d; print "$start_input\n"; $end_input =~ tr/\.//d; print "$end_input\n"; while (<DATA>){ # I don't see this ever working. Did you mean $start_input =~ /\d +{4}/ .. ? if (/$start_input\d\d\d\d/ .. /$end_input\d\d\d\d/){ if ($_ =~ /(.*)\t(.*)\t(.*)/){ print "Date = $1" . "\t" . "Temp_moy_DegCs = $2" . "\t" . "HR +_moy_s = $3\n"; } } } } close (DATA);

Dave


In reply to Re: Consecutive if loops by davido
in thread Consecutive if loops by M15U

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.