Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

<if i want the last digit of numbers to always match a pre-determined number how do i achieve that>?

Replies are listed 'Best First'.
Re: <digits match>
by atcroft (Abbot) on Jul 10, 2015 at 19:30 UTC

    End the match pattern with the number you are seeking, such as

    print "3-digits ending in 5" if ( $number =~ m/\d{2}5$/ ); print "4-digit number ending in 0 or 9" if ( $number =~ m/\d{3}[09]$/ ); # as opposed to print "2 digits followed by a 3, but may have additional digits" if ( $number =~ m/\d{2}3/ );

    Hope that helps.

Re: <digits match>
by choroba (Cardinal) on Jul 10, 2015 at 23:13 UTC
    You can also use substr with a negative offset:
    my $last_digit = 5; if ($last_digit == substr $number, -1) { print "Matches!\n" }

    Or, similarly, use rindex:

    if (rindex($number, $last_digit) == length($number) - 1) { print "Matches!\n" }
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: <digits match>
by stevieb (Canon) on Jul 10, 2015 at 19:22 UTC

    There are many ways. Start here: How do I post a question effectively?

    #!/usr/bin/perl use warnings; use strict; my $predetermined = 6; my @nums = qw(6 645 1086 1996 2015 2026); for my $num (@nums){ if ($num =~ /$predetermined$/){ print "$num\n"; } }

    -stevieb