M15U has asked for the wisdom of the Perl Monks concerning the following question:
Hello monks
I new to perl and I can't really tell why this code is not working :
#!/usr/bin/perl -w use strict; use locale; use warnings; #use diagnostics; use utf8; binmode(STDIN, "encoding(utf8)"); binmode(STDOUT, "encoding(utf8)"); binmode(STDERR, "encoding(utf8)"); my $data_file = "data.txt"; my ($start, $end); open (DATA, '<:utf8', $data_file) || die "Couldn't open $data_file : $ +!\n"; 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>); if ($start_input =~ /\d\d\d\d/ && $end_input =~ /\d\d\d\d/){ while (<DATA>){ 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/ +){ $start_input =~ tr/\.//d; print "$start_input\n"; $end_input =~ tr/\.//d; print "$end_input\n"; while (<DATA>){ 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);
When I comment one of the first "if" loops it works just fine for one of the date formats ('yyyy' or 'yyyy.mm'). I also tried using the "elsif", but it's the same thing. Also it still doesn't work by executing all the "if" statments in the same "while" loop.
The source file looks like this
1998032602 -6 77 1998032603 -7 82 1998032604 -7,8 81 1998032605 -8,3 82 1998032606 -8,6 85 1998032607 -2,5 62 1998032608 2,1 37 1998032609 5,5 25 1998032610 8,9 16 1998032611 11 16 1998032612 12,9 15 1998032613 13,3 13 1998032614 13,4 14 1998032615 13,1 16 1999093022 7,2 96 1999093023 5,6 98 1999100100 5,7 99 1999100101 5,9 99 1999100102 5,4 99 1999100103 5,9 99 1999100104 5,9 99 1999100105 5,5 99 1999100106 4,2 99 1999100107 6 99 1999100108 9,3 82 1999100109 12,1 64 1999100110 14,5 50 1999100111 16,5 47 1999100112 17,8 49 1999100113 18,5 49 1999100114 18,6 48
Thank you
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Consecutive if loops
by tobyink (Canon) on Mar 18, 2013 at 16:56 UTC | |
|
Re: Consecutive if loops
by davido (Cardinal) on Mar 18, 2013 at 17:12 UTC | |
|
Re: Consecutive if loops
by poj (Abbot) on Mar 18, 2013 at 16:51 UTC | |
|
Re: Consecutive if loops
by Jim (Curate) on Mar 18, 2013 at 16:35 UTC | |
by M15U (Acolyte) on Mar 18, 2013 at 16:51 UTC | |
|
Re: Consecutive if loops
by hdb (Monsignor) on Mar 18, 2013 at 16:59 UTC |