How can I get the last two digits of a number using Perl?

How can I get a value from a specific number?

Assume a number 20040819. I want to get the last two digits of ie 19using Perl.

+3
source share
9 answers
my $x = 20040819;
print $x % 100, "\n";
print substr($x, -2);
+11
source

Benoit's answer is at the mark and one that I would use, but for this, using a template search, as you suggested in your title, you should:

my $x = 20040819;
if ($x =~ /\d*(\d{2})/)
{
    $lastTwo = $1;
}
+4
source
substr("20040819", -2); 

Regexp:: 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
}
+3

, YYYYMMDD , :

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

defined $year .., , .

+3
my $num = 20040819;
my $i = 0;
if ($num =~ m/([0-9]{2})$/) {
    $i = $1;
}
print $i;
+2
source

Another option:

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

\b matches the word boundary.

+1
source

Another solution:

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

The solution for you:

my $number = 20040819;
my ($pick) = $number =~ m/(\d{2})$/;
print "$pick\n";
0
source
$x=20040819-int(20040819/100)*100;
-1
source

Source: https://habr.com/ru/post/1767517/


All Articles