如何使用 Perl 获取数字的最后两位数字?

发布于 2024-09-25 20:37:57 字数 92 浏览 3 评论 0原文

如何从特定数字中获取值?

假设号码是20040819。我想使用 Perl 获取最后两位数字,即 19

How can I get a value from particular number?

Lets say number is 20040819. I want to get last two digit i.e 19 using Perl.

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

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

发布评论

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

评论(9

野稚 2024-10-02 20:37:57
my $x = 20040819;
print $x % 100, "\n";
print substr($x, -2);
my $x = 20040819;
print $x % 100, "\n";
print substr($x, -2);
情深如许 2024-10-02 20:37:57

伯努瓦的答案是正确的,我会使用这个答案,但是为了按照您在标题中建议的那样通过模式搜索来做到这一点,您可以这样做:

my $x = 20040819;
if ($x =~ /\d*(\d{2})/)
{
    $lastTwo = $1;
}

Benoit's answer is on the mark and one I would use, but in order to do this with a pattern search as you suggested in your title, you would do:

my $x = 20040819;
if ($x =~ /\d*(\d{2})/)
{
    $lastTwo = $1;
}
帝王念 2024-10-02 20:37:57

我将进一步展示如何将 YYYYMMDD 格式的日期提取为年、月和日期:

my $str = '20040819';
my ($year, $month, $date) = $str =~ /^(\d{4})(\d{2})(\d{2})$/;

您可以检查 define $year 等,以确定是否匹配工作与否。

I'm just going to go beyond and show how to extract a YYYYMMDD format date into a year, month, and date:

my $str = '20040819';
my ($year, $month, $date) = $str =~ /^(\d{4})(\d{2})(\d{2})$/;

You can check for defined $year, etc., to figure out if the match worked or not.

む无字情书 2024-10-02 20:37:57
substr("20040819", -2); 

或者您可以使用 正则表达式::Common::time - 日期和时间正则表达式,例如

use strict;
use Regexp::Common qw(time);


my $str = '20040819' ;

if ($str =~ $RE{time}{YMD}{-keep})
{
  my $day = $4; # output 19

  #$1 the entire match

  #$2 the year

  #$3 the month

  #$4 the day
}
substr("20040819", -2); 

or you can use Regexp::Common::time - Date and time regular expressions like

use strict;
use Regexp::Common qw(time);


my $str = '20040819' ;

if ($str =~ $RE{time}{YMD}{-keep})
{
  my $day = $4; # output 19

  #$1 the entire match

  #$2 the year

  #$3 the month

  #$4 the day
}
∝单色的世界 2024-10-02 20:37:57
my $num = 20040819;
my $i = 0;
if ($num =~ m/([0-9]{2})$/) {
    $i = $1;
}
print $i;
my $num = 20040819;
my $i = 0;
if ($num =~ m/([0-9]{2})$/) {
    $i = $1;
}
print $i;
失退 2024-10-02 20:37:57

另一种选择:

my $x = 20040819;
$x =~ /(\d{2})\b/;
my $last_two_digits = $1;

\b 匹配单词边界。

Another option:

my $x = 20040819;
$x =~ /(\d{2})\b/;
my $last_two_digits = $1;

the \b matches a word boundary.

审判长 2024-10-02 20:37:57

还有一个解决方案:

my $number = '20040819';
my @digits = split //, $number;
print join('', splice @digits, -2, 2);

Yet another solution:

my $number = '20040819';
my @digits = split //, $number;
print join('', splice @digits, -2, 2);
乖不如嘢 2024-10-02 20:37:57

为您提供的解决方案:

my $number = 20040819;
my ($pick) = $number =~ m/(\d{2})$/;
print "$pick\n";

Solution for you:

my $number = 20040819;
my ($pick) = $number =~ m/(\d{2})$/;
print "$pick\n";
明明#如月 2024-10-02 20:37:57
$x=20040819-int(20040819/100)*100;
$x=20040819-int(20040819/100)*100;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文