Perl 中的 4 位数字验证

发布于 2024-11-24 11:46:58 字数 590 浏览 1 评论 0 原文

应该小于第二个传递的参数,并且两个参数都应该只是数字,而且它们应该恰好是 4。

当我将命令行参数传递给 perl 程序时,我进行了验证检查。我通过了两年,第一个传递的参数 >#Sunny>perl check.pl 2007 2008 这很好

#Sunny>perl check.pl 2008 2007 这很糟糕

#Sunny>perl check.pl 200 2007 这很糟糕

我已经为此编写了代码,但无法弄清楚为什么它不起作用。

#!usr/bin/perl
#check.pl

if ($#ARGV < 0) { }
else
{
  $fiscyear1 = $ARGV[0];
  $fiscyear2 = $ARGV[1];
}

if (($fiscyear1 !~ m/\d{4}/) and ($fiscyear2 !~ m/\d{4}/) and ($fiscyear1 < $fiscyear2))
{ print "Bad parameters\n"; }
else
{ print "good parameters\n"; }

I have a validation check when I pass the command line parameters to the perl program.I pass two years first passed argument should be lesser than the second passed argument and both the arguments should be only digits and also they should be exactly 4.

#Sunny>perl check.pl 2007 2008 This is good

#Sunny>perl check.pl 2008 2007 This is bad

#Sunny>perl check.pl 200 2007 This is bad

I have written code for this but not able to figure it out why its not working.

#!usr/bin/perl
#check.pl

if ($#ARGV < 0) { }
else
{
  $fiscyear1 = $ARGV[0];
  $fiscyear2 = $ARGV[1];
}

if (($fiscyear1 !~ m/\d{4}/) and ($fiscyear2 !~ m/\d{4}/) and ($fiscyear1 < $fiscyear2))
{ print "Bad parameters\n"; }
else
{ print "good parameters\n"; }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

戈亓 2024-12-01 11:46:58

这听起来像是一个过度思考的糟糕情况......

您想要验证参数是否为两年,采用 YYYY 形式,并且第二年在第一年之后。

嗯,9999 可以通过任何数字检查,但它几乎不是一个有效的年份。与 0101 或 3021 等相同。

您想要的是设置给定范围,并确保参数是数字。

use strict;
use warnings;

my $yr1 = shift;
my $yr2 = shift || usage();

usage() unless $yr1 =~ /^\d+$/;
usage() unless $yr2 =~ /^\d+$/;

my $min_year = 1900;
my $max_year = 2200;

if ( ($yr1 < $min_year) or ($yr1 > $max_year) ) {
    die "Invalid year: $yr1";
}
if ( ($yr2 < $min_year) or ($yr2 > $max_year) ) {
    die "Invalid year: $yr2";
}
if ( $yr1 >= $yr2 ) {
    die "Invalid sequence: $yr2 is not greater than $yr1";
}

sub usage {
    die "Usage script.pl <year1> <year2>";
}

This sounds like a bad case of overthinking things...

What you want is to verify that the arguments are two years, in the form YYYY, and that the second year comes after the first.

Well, 9999 would pass any digit check, but it is hardly a valid year. Same with 0101, or 3021, etc.

What you want is to set a given range, and make sure the arguments are numeric.

use strict;
use warnings;

my $yr1 = shift;
my $yr2 = shift || usage();

usage() unless $yr1 =~ /^\d+$/;
usage() unless $yr2 =~ /^\d+$/;

my $min_year = 1900;
my $max_year = 2200;

if ( ($yr1 < $min_year) or ($yr1 > $max_year) ) {
    die "Invalid year: $yr1";
}
if ( ($yr2 < $min_year) or ($yr2 > $max_year) ) {
    die "Invalid year: $yr2";
}
if ( $yr1 >= $yr2 ) {
    die "Invalid sequence: $yr2 is not greater than $yr1";
}

sub usage {
    die "Usage script.pl <year1> <year2>";
}
没有伤那来痛 2024-12-01 11:46:58

怎么样:

if (($fiscyear1 !~ m/^\d{4}$/) or ($fiscyear2 !~ m/^\d{4}$/) or ($fiscyear1 > $fiscyear2))
   { print "Bad parameters\n"; }

我将 or 中的 and 以及最终的 < 更改为 > (因为你希望第一个参数小于第二个)

编辑:

它似乎适用于我的情况:

我也非常强烈地接受使用建议^$,并相应地修改了我的答案。

What about this:

if (($fiscyear1 !~ m/^\d{4}$/) or ($fiscyear2 !~ m/^\d{4}$/) or ($fiscyear1 > $fiscyear2))
   { print "Bad parameters\n"; }

I changed the ands in ors and also the final < into > (since you want the first argument is less than the second)

EDIT:

It seems that it works in my case:

enter image description here

I also very strongly accept the advice of using ^$, and have modified my answer accordingly.

哎呦我呸! 2024-12-01 11:46:58

您在正则表达式中缺少字符串 ^$ 的开头和结尾(没有它们,它可以匹配 5 个或更多符号):

use strict; use warnings;
my ($fiscyear1, $fiscyear2) = @ARGV;
if ($fiscyear1 =~ m{^\d{4}$} && $fiscyear2 =~ m{^\d{4}$} && $fiscyear1 < $fiscyear2) {
    print "good parameters\n";
} else {
    print "Bad parameters\n";
}

更新
您还可以在此处使用unless,例如:

unless ($fiscyear1 =~ m{^\d{4}$} && $fiscyear2 =~ m{^\d{4}$} && $fiscyear1 < $fiscyear2) {
    print "Bad parameters\n";
    exit(1); ## tells the caller side that there is an error
}
print "good parameters\n";

You are missing start and end of string ^ and $ in regexps (without them it can match 5 or more symbols):

use strict; use warnings;
my ($fiscyear1, $fiscyear2) = @ARGV;
if ($fiscyear1 =~ m{^\d{4}$} && $fiscyear2 =~ m{^\d{4}$} && $fiscyear1 < $fiscyear2) {
    print "good parameters\n";
} else {
    print "Bad parameters\n";
}

Update
You can also use unless here like:

unless ($fiscyear1 =~ m{^\d{4}$} && $fiscyear2 =~ m{^\d{4}$} && $fiscyear1 < $fiscyear2) {
    print "Bad parameters\n";
    exit(1); ## tells the caller side that there is an error
}
print "good parameters\n";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文