Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从给定日期查找日期 #/usr/bin/perl @月份=(31,28,31,30,31,30,31,31,30,31,31,30,31,30,31); @周=(“星期日”、“星期一”、“星期二”、“星期三”、“星期四”、“星期五”, “星期六”); 打印“日期:\n”; $date=; 打印“mon:\n”; $mon=; 打印“年份:\n”; $year=; 如果($year%400==0)| |($year%4==0)&($year%100!=0)) { $month[1]=29; 对于($i=0;$i_Perl - Fatal编程技术网

从给定日期查找日期 #/usr/bin/perl @月份=(31,28,31,30,31,30,31,31,30,31,31,30,31,30,31); @周=(“星期日”、“星期一”、“星期二”、“星期三”、“星期四”、“星期五”, “星期六”); 打印“日期:\n”; $date=; 打印“mon:\n”; $mon=; 打印“年份:\n”; $year=; 如果($year%400==0)| |($year%4==0)&($year%100!=0)) { $month[1]=29; 对于($i=0;$i

从给定日期查找日期 #/usr/bin/perl @月份=(31,28,31,30,31,30,31,31,30,31,31,30,31,30,31); @周=(“星期日”、“星期一”、“星期二”、“星期三”、“星期四”、“星期五”, “星期六”); 打印“日期:\n”; $date=; 打印“mon:\n”; $mon=; 打印“年份:\n”; $year=; 如果($year%400==0)| |($year%4==0)&($year%100!=0)) { $month[1]=29; 对于($i=0;$i,perl,Perl,几天来,我一直在尝试学习perl,我编写了这些代码来查找给定日期的日期。实际上我是用C代码转换的。但事实并非如此。输出总是星期一。我在哪里犯错 #!/usr/bin/perl @month = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); @week = ("Sunday", "Monday","Tuesday", "Wednesday","Thursday", "Friday", "Saturday"); print "dat

几天来,我一直在尝试学习perl,我编写了这些代码来查找给定日期的日期。实际上我是用C代码转换的。但事实并非如此。输出总是星期一。我在哪里犯错

#!/usr/bin/perl

@month = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
@week = ("Sunday", "Monday","Tuesday", "Wednesday","Thursday", "Friday", 
    "Saturday");

print "date:\n";
$date=<STDIN>;
print "mon:\n";
$mon=<STDIN>;
print "year:\n";
$year=<STDIN>;

if ( ($year % 400 == 0) || ($year % 4 == 0) && ($year % 100 != 0) )
{
    $month[1] = 29 ;
    for($i = 0 ; $i < $mon - 1 ; $i++)
    {       
        $s = $s + $month[$i] ;
        $s = $s + ($date + $year + ($year / 4) - 2) ;
        $s = $s % 7 ;
    }
}
print $week[$s+1] ;
更好的是

use Time::Local qw( timegm );

my @dow_names = qw( Sunday Monday Tuesday Wednesday Thursday Friday Saturday );

my $Y = ...;
my $m = ...;
my $d = ...;

my $epoch = timegm(0, 0, 0, $d, $m-1, $Y-1900);
my $dow = ( gmtime($epoch) )[6];
my $dow_name = $dow_names[$dow];

你也可以使用;它使用起来更简单,但它不像上面使用的模块那么轻。

正如@Chris Turner所指出的,您的代码中没有路径来处理非闰年的情况。要解决这一问题并实现您声明的目标,只需对代码进行一个小的更改

use POSIX       qw( strftime );
use Time::Local qw( timegm );

my $Y = ...;
my $m = ...;
my $d = ...;

my $epoch = timegm(0, 0, 0, $d, $m-1, $Y-1900);
my $dow_name = strftime("%A", gmtime($epoch));

因为这些杂注促进了良好的编程实践,并有助于在开发的早期发现许多错误。他们不会帮你解决这个问题,但如果你开始使用他们,他们会帮你解决下一个问题。

要介绍一个最广泛使用的日期和时间模块,这里有

这是一个非常大且“沉重”的模块,具有很多功能。还有其他的


虽然我支持将手工操作作为学习的一部分,但一旦涉及到日期和时间,您将希望使用模块。

不要自己动手。使用模块。已经成为Perl发行版的标准部分将近十年了

use warnings;
use strict;
use feature qw(say);

use DateTime;

# ... acquire input ($year, $mon, $date)

my $dt = DateTime->new(year => $year, month => $mon, day => $date);

say $dt->day_name;
当它应该是这样的时候:

#!/usr/bin/perl

use strict;
use warnings;
use feature 'say';

use Time::Piece;

print "date:\n";
chomp(my $date = <STDIN>);
print "mon:\n";
chomp(my $mon = <STDIN>);
print "year:\n";
chomp(my $year = <STDIN>);

my $tp = Time::Piece->strptime("$year-$mon-$date", '%Y-%m-%d');

say $tp->fullday;
if (we're in a leap year) {
    Change the @months array to deal with leap years
    Do the maths to calculate the day
}
if (we're in a leap year) {
    Change the @months array to deal with leap years
}
Do the maths to calculate the day
因此,除非您的输入年份是闰年,否则您将跳过所有计算。这意味着$s从未被赋予价值。Perl将未定义的值视为0,因此您的最终语句总是打印星期一的
$week[0+1]

如果像Time::Piece这样的模块不可用,Perl程序员会这样编写代码:

#!/usr/bin/perl

use strict;
use warnings;
use feature 'say';

use Time::Piece;

print "date:\n";
chomp(my $date = <STDIN>);
print "mon:\n";
chomp(my $mon = <STDIN>);
print "year:\n";
chomp(my $year = <STDIN>);

my $tp = Time::Piece->strptime("$year-$mon-$date", '%Y-%m-%d');

say $tp->fullday;
if (we're in a leap year) {
    Change the @months array to deal with leap years
    Do the maths to calculate the day
}
if (we're in a leap year) {
    Change the @months array to deal with leap years
}
Do the maths to calculate the day
#/usr/bin/perl
#强制我们声明变量。
严格使用;
#让Perl在我们做蠢事时告诉我们
使用警告;
#允许使用say()
使用特征“说”;
#用my()声明变量
我的@月份=(31,28,31,30,31,30,31,31,30,31,30,31,30,31);
#qw(…)定义了一个没有所有繁琐标点符号的列表
my@week=qw(星期日、星期一、星期二、星期三、星期五、星期六);
打印“日期:\n”;
#使用chomp()从输入中删除换行符
chomp(我的$date=);
打印“mon:\n”;
chomp(我的$mon=);
打印“年份:\n”;
chomp(我的$year=);
#这种逻辑可以被澄清很多。
如果($year%400==0)| |($year%4==0)&($year%100!=0)){
$month[1]=29;
}
#初始化$s以避免以后出现警告
我的$s=0;
#foreach外观几乎总是比(;)更干净
每个我的$i(0..$mon-2){
#还没有检查你的计算结果(但它们似乎有效)
#+=是有用的快捷方式
$s+=$month[$i];
$s+=($date+$year+($year/4)-2);
$s%=7;
}
#say()类似于print(),但有一个额外的换行符
比如说$week[$s+1];

你应该缩进你的代码,因为它很难阅读,但是如果
$year
不是闰年,我看不到它在哪里更新了
$s
?我不能使用本地时间,我必须计算日期。但是谢谢你,这不是很合理。正如你已经注意到的,它需要更多的工作,更容易出错。这是一个好的、易懂的例子顺便说一句,我认为OP更多的是在重新发明轮子以了解轮子如何工作之后,而不是在计算日期值的库存模块之后。我喜欢这样。
#!/usr/bin/perl

# Force us to declare variables.
use strict;
# Get Perl to tell us when we're doing something stupid
use warnings;
# Allow the use of say()
use feature 'say';

# Declare variables with my()
my @month = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
# qw(...) defines a list without all that tedious punctuation
my @week = qw(Sunday Monday Tuesday Wednesday Thursday Friday Saturday);

print "date:\n";
# Use chomp() to remove newlines from input
chomp(my $date = <STDIN>);
print "mon:\n";
chomp(my $mon = <STDIN>);
print "year:\n";
chomp(my $year = <STDIN>);

# This logic can be cleaned up a lot.
if ( ($year % 400 == 0) || ($year % 4 == 0) && ($year % 100 != 0) ) {
    $month[1] = 29 ;
}

# Initialise $s to avoid warnings later
my $s = 0;
# A foreach look is almost always cleaner than for (;;)
foreach my $i (0 .. $mon - 2) {
    # Haven't checked your calculations (but they seem to work
    # += is  useful shortcut
    $s += $month[$i];
    $s += ($date + $year + ($year / 4) - 2);
    $s %= 7;
}

# say() is like print() but with an extra newline
say $week[$s+1];