FAQ
Hey everyone,

I am trying to get just the last 3 numbers from the following number
from perl using regexs but I have not had no success so I was hoping
that I could get a little help on this. I just ordered the regex book
from oreilly so that hopefully in the future I will not have to ask a
question like this again.

The number is 0000000000000111

I was just wanting to get the last 3 digits from this number to be
able to get an exact word phrase from my already loaded Hash table
that I have created for various numbers that I will get the last 3
numbers from. Oh yeah all I was doing was putting this number in a
file and then passing it to perl on the command line incase that is
needed.

Any help is much appreciated or if you can just point me to a good
place to get a better understanding of how to do this while I wait for
my book to arrive.

Thanks,
shadow52

Search Discussions

  • Alan Haggai Alavi at Nov 23, 2009 at 12:58 pm
    Hi,

    The following should work:

    my $number = '0000000000000111';
    my ($index) = ( $number =~ /\d+(\d{3})/ );

    $index would contain the last three digits: '111'.

    To learn about regular expressions in Perl, you can go through `perldoc
    perlre` and `perldoc perlretut`. The Perldoc website
    (http://perldoc.perl.org/) can be of help as well.

    Regards,
    Alan Haggai Alavi.
  • Philip Potter at Nov 24, 2009 at 3:16 pm

    2009/11/23 Alan Haggai Alavi <alanhaggai@alanhaggai.org>:
    my $number = '0000000000000111';
    my ($index) = ( $number =~ /\d+(\d{3})/ );

    $index would contain the last three digits: '111'.
    \d+ should be \d* above, otherwise you fail to match on 3 digit numbers:

    pgp@tui:~/tmp$ perl -E '
    my $number = q{111};
    my ($index) = ( $number =~ /\d+(\d{3})/ );
    say $index'

    pgp@tui:~/tmp$

    Phil
  • Gaochong at Nov 23, 2009 at 1:00 pm
    substr
    but you have to set it to be char .

    can I help you ?
    -----邮件原件-----
    发件人: shadow52
    发送时间: 2009年11月23日 11:27
    收件人: beginners@perl.org
    主题: Regex to get last 3 digits of a number.

    Hey everyone,

    I am trying to get just the last 3 numbers from the following number
    from perl using regexs but I have not had no success so I was hoping
    that I could get a little help on this. I just ordered the regex book
    from oreilly so that hopefully in the future I will not have to ask a
    question like this again.

    The number is 0000000000000111

    I was just wanting to get the last 3 digits from this number to be
    able to get an exact word phrase from my already loaded Hash table
    that I have created for various numbers that I will get the last 3
    numbers from. Oh yeah all I was doing was putting this number in a
    file and then passing it to perl on the command line incase that is
    needed.

    Any help is much appreciated or if you can just point me to a good
    place to get a better understanding of how to do this while I wait for
    my book to arrive.

    Thanks,
    shadow52


    --
    To unsubscribe, e-mail: beginners-unsubscribe@perl.org
    For additional commands, e-mail: beginners-help@perl.org
    http://learn.perl.org/
  • Shawn H Corey at Nov 23, 2009 at 1:14 pm

    shadow52 wrote:
    Hey everyone,

    I am trying to get just the last 3 numbers from the following number
    from perl using regexs but I have not had no success so I was hoping
    that I could get a little help on this. I just ordered the regex book
    from oreilly so that hopefully in the future I will not have to ask a
    question like this again.

    The number is 0000000000000111

    I was just wanting to get the last 3 digits from this number to be
    able to get an exact word phrase from my already loaded Hash table
    that I have created for various numbers that I will get the last 3
    numbers from. Oh yeah all I was doing was putting this number in a
    file and then passing it to perl on the command line incase that is
    needed.

    Any help is much appreciated or if you can just point me to a good
    place to get a better understanding of how to do this while I wait for
    my book to arrive.

    Thanks,
    shadow52
    my $n = "0000000000000111";
    my ( $last_3_digits ) = $n =~ m{ (\d\d\d) \z }msx;
    print "$last_3_digits\n";


    --
    Just my 0.00000002 million dollars worth,
    Shawn

    Programming is as much about organization and communication
    as it is about coding.

    I like Perl; it's the only language where you can bless your
    thingy.
  • Dave Tang at Nov 23, 2009 at 11:34 pm

    On Mon, 23 Nov 2009 23:14:51 +1000, Shawn H Corey wrote:

    shadow52 wrote:
    [snip]
    The number is 0000000000000111

    I was just wanting to get the last 3 digits from this number to be
    able to get an exact word phrase from my already loaded Hash table
    that I have created for various numbers that I will get the last 3
    numbers from. Oh yeah all I was doing was putting this number in a
    file and then passing it to perl on the command line incase that is
    needed.
    [snip]
    my $n = "0000000000000111";
    my ( $last_3_digits ) = $n =~ m{ (\d\d\d) \z }msx;
    print "$last_3_digits\n";
    Hi Shawn et al.,

    I am also intrigued by the \z anchor. I had a look at perldoc perlreref
    and found

    \z Match absolute string end

    My question is what is the difference between \z and $? And when should I
    use \z rather than $ ? From the same documentation:

    $ Matches at the end of the string (or line, if /m is used)

    Cheers,

    Dave
  • Dermot at Nov 23, 2009 at 11:39 pm

    2009/11/23 Dave Tang <d.tang@imb.uq.edu.au>:
    On Mon, 23 Nov 2009 23:14:51 +1000, Shawn H Corey <shawnhcorey@gmail.com>

    Hi Shawn et al.,

    I am also intrigued by the \z anchor. I had a look at perldoc perlreref and
    found

    \z  Match absolute string end

    My question is what is the difference between \z and $? And when should I
    use \z rather than $ ? From the same documentation:

    $   Matches at the end of the string (or line, if /m is used)
    From perlre

    The \A and \Z are just like "^" and "$", except that they won't
    match multiple times when the /m modifier is used, while "^" and "$"
    will match at every internal line boundary. To match the actual end of
    the string and not ignore an optional trailing newline, use \z .

    HTH,
    Dp.
  • Dermot at Nov 24, 2009 at 8:33 am

    2009/11/23 Dave Tang <d.tang@imb.uq.edu.au>:
    On Tue, 24 Nov 2009 09:39:09 +1000, Dermot wrote:

    2009/11/23 Dave Tang <d.tang@imb.uq.edu.au>:
    On Mon, 23 Nov 2009 23:14:51 +1000, Shawn H Corey <shawnhcorey@gmail.com>

    Hi Shawn et al.,

    I am also intrigued by the \z anchor. I had a look at perldoc perlreref
    and
    found

    \z  Match absolute string end

    My question is what is the difference between \z and $? And when should I
    use \z rather than $ ? From the same documentation:

    $   Matches at the end of the string (or line, if /m is used)
    From perlre

    The \A  and \Z  are just like "^" and "$", except that they won't
    match multiple times when the /m modifier is used, while "^" and "$"
    will match at every internal line boundary. To match the actual end of
    the string and not ignore an optional trailing newline, use \z .
    Thanks Dermot! I guess I should RTFM a bit more next time, before posting a
    question.

    Lesson learnt :P
    Lol, I only know because I asked the same question myself (on a
    different list) and got pretty much the same style answer. I wouldn't
    have found it on my own.
    Good luck,
    Dp.
  • Dave Tang at Nov 24, 2009 at 12:57 pm

    On Tue, 24 Nov 2009 18:33:30 +1000, Dermot wrote:

    2009/11/23 Dave Tang <d.tang@imb.uq.edu.au>:
    On Tue, 24 Nov 2009 09:39:09 +1000, Dermot <paikkos@googlemail.com>
    wrote:
    2009/11/23 Dave Tang <d.tang@imb.uq.edu.au>:
    On Mon, 23 Nov 2009 23:14:51 +1000, Shawn H Corey
    <shawnhcorey@gmail.com>

    Hi Shawn et al.,

    I am also intrigued by the \z anchor. I had a look at perldoc
    perlreref
    and
    found

    \z Match absolute string end

    My question is what is the difference between \z and $? And when
    should I
    use \z rather than $ ? From the same documentation:

    $ Matches at the end of the string (or line, if /m is used)
    From perlre

    The \A and \Z are just like "^" and "$", except that they won't
    match multiple times when the /m modifier is used, while "^" and "$"
    will match at every internal line boundary. To match the actual end of
    the string and not ignore an optional trailing newline, use \z .
    Thanks Dermot! I guess I should RTFM a bit more next time, before
    posting a
    question.

    Lesson learnt :P
    Lol, I only know because I asked the same question myself (on a
    different list) and got pretty much the same style answer. I wouldn't
    have found it on my own.
    Good luck,
    Dp.
    Hehe I just thought it was funny that I read the manual to find out what
    \z meant but it didn't occur to me read more about it from the manual :P

    Which was why I was a bit hard on myself.

    Thanks again!

    Dave
  • Rob Coops at Nov 23, 2009 at 1:16 pm

    On Mon, Nov 23, 2009 at 4:27 AM, shadow52 wrote:

    Hey everyone,

    I am trying to get just the last 3 numbers from the following number
    from perl using regexs but I have not had no success so I was hoping
    that I could get a little help on this. I just ordered the regex book
    from oreilly so that hopefully in the future I will not have to ask a
    question like this again.

    The number is 0000000000000111

    I was just wanting to get the last 3 digits from this number to be
    able to get an exact word phrase from my already loaded Hash table
    that I have created for various numbers that I will get the last 3
    numbers from. Oh yeah all I was doing was putting this number in a
    file and then passing it to perl on the command line incase that is
    needed.

    Any help is much appreciated or if you can just point me to a good
    place to get a better understanding of how to do this while I wait for
    my book to arrive.

    Thanks,
    shadow52


    --
    To unsubscribe, e-mail: beginners-unsubscribe@perl.org
    For additional commands, e-mail: beginners-help@perl.org
    http://learn.perl.org/

    Hi shadow52,

    The trick with subsrt works of course and there is no need to set this as
    character as this is perl where we don't care what is in the scalar (number,
    character or string), the programming language deals with that for us so we
    just need to be concerned with the interesting things like how do we do this
    in a different way...

    With a regular expression you can use the $ to signify the end of the string
    so a regulat expression catching the following:
    $your_long_number =~ /.*(\d{3})$/;
    print "My 3 digits are: $1\n";

    Just saw Shawn's solution which I assume will work as well though I am not
    sure what the \z is for looking that up :-)

    Regards,

    Rob
  • John W. Krahn at Nov 23, 2009 at 3:23 pm

    shadow52 wrote:
    Hey everyone, Hello,
    I am trying to get just the last 3 numbers from the following number
    from perl using regexs but I have not had no success so I was hoping
    that I could get a little help on this. I just ordered the regex book
    from oreilly so that hopefully in the future I will not have to ask a
    question like this again.

    The number is 0000000000000111

    I was just wanting to get the last 3 digits from this number to be
    able to get an exact word phrase from my already loaded Hash table
    that I have created for various numbers that I will get the last 3
    numbers from.
    To get the last three characters:

    $ perl -le'my $number = "0000000000000111"; my ( $last_3 ) = $number =~
    /(...)$/; print $last_3'
    111

    Or:

    $ perl -le'my $number = "0000000000000111"; my $last_3 = substr $number,
    -3; print $last_3'
    111

    Of course the regex version will ignore any newline at the end.

    To get the last three numerical digits only:

    $ perl -le'my $number = "0000000000000111"; my ( $last_3 ) = $number =~
    /(\d\d\d)$/; print $last_3'
    111

    Or:

    $ perl -le'my $number = "0000000000000111"; my ( $last_3 ) = $number =~
    /.*(\d\d\d)/; print $last_3'
    111


    \d\d\d could also be written as \d{3}.

    Oh yeah all I was doing was putting this number in a
    file and then passing it to perl on the command line incase that is
    needed.
    I can not think of why that may be needed?



    John
    --
    The programmer is fighting against the two most
    destructive forces in the universe: entropy and
    human stupidity. -- Damian Conway
  • Shadow52 at Nov 23, 2009 at 5:49 pm

    On Nov 23, 9:22 am, jwkr...@shaw.ca (John W. Krahn) wrote:
    shadow52 wrote:
    Hey everyone, Hello,
    I am trying to get just the last 3 numbers from the following number
    from perl using regexs but I have not had no success so I was hoping
    that I could get a little help on this. I just ordered the regex book
    from oreilly so that hopefully in the future I will not have to ask a
    question like this again.
    The number is 0000000000000111
    I was just wanting to get the last 3 digits from this number to be
    able to get an exact word phrase from my already loaded Hash table
    that I have created for various numbers that I will get the last 3
    numbers from.
    To get the last three characters:

    $ perl -le'my $number = "0000000000000111"; my ( $last_3 ) = $number =~
    /(...)$/; print $last_3'
    111

    Or:

    $ perl -le'my $number = "0000000000000111"; my $last_3 = substr $number,
    -3; print $last_3'
    111

    Of course the regex version will ignore any newline at the end.

    To get the last three numerical digits only:

    $ perl -le'my $number = "0000000000000111"; my ( $last_3 ) = $number =~
    /(\d\d\d)$/; print $last_3'
    111

    Or:

    $ perl -le'my $number = "0000000000000111"; my ( $last_3 ) = $number =~
    /.*(\d\d\d)/; print $last_3'
    111

    \d\d\d could also be written as \d{3}.
    Oh yeah all I was doing was putting this number in a
    file and then passing it to perl on the command line incase that is
    needed.
    I can not think of why that may be needed?

    John
    --
    The programmer is fighting against the two most
    destructive forces in the universe: entropy and
    human stupidity.               -- Damian Conway
    I just wanted to thank everyone who replied all of them worked thanks
    for the help. John thanks for all of the diffrent wanys to do this
    also.
  • Shadow52 at Nov 23, 2009 at 5:49 pm

    On Nov 23, 9:22 am, jwkr...@shaw.ca (John W. Krahn) wrote:
    shadow52 wrote:
    Hey everyone, Hello,
    I am trying to get just the last 3 numbers from the following number
    from perl using regexs but I have not had no success so I was hoping
    that I could get a little help on this. I just ordered the regex book
    from oreilly so that hopefully in the future I will not have to ask a
    question like this again.
    The number is 0000000000000111
    I was just wanting to get the last 3 digits from this number to be
    able to get an exact word phrase from my already loaded Hash table
    that I have created for various numbers that I will get the last 3
    numbers from.
    To get the last three characters:

    $ perl -le'my $number = "0000000000000111"; my ( $last_3 ) = $number =~
    /(...)$/; print $last_3'
    111

    Or:

    $ perl -le'my $number = "0000000000000111"; my $last_3 = substr $number,
    -3; print $last_3'
    111

    Of course the regex version will ignore any newline at the end.

    To get the last three numerical digits only:

    $ perl -le'my $number = "0000000000000111"; my ( $last_3 ) = $number =~
    /(\d\d\d)$/; print $last_3'
    111

    Or:

    $ perl -le'my $number = "0000000000000111"; my ( $last_3 ) = $number =~
    /.*(\d\d\d)/; print $last_3'
    111

    \d\d\d could also be written as \d{3}.
    Oh yeah all I was doing was putting this number in a
    file and then passing it to perl on the command line incase that is
    needed.
    I can not think of why that may be needed?

    John
    --
    The programmer is fighting against the two most
    destructive forces in the universe: entropy and
    human stupidity.               -- Damian Conway
    I just wanted to thank everyone who replied all of them worked thanks
    for the help. John thanks for all of the diffrent wanys to do this
    also.
  • Shadow52 at Nov 23, 2009 at 10:06 pm

    On Nov 23, 9:22 am, jwkr...@shaw.ca (John W. Krahn) wrote:
    shadow52 wrote:
    Hey everyone, Hello,
    I am trying to get just the last 3 numbers from the following number
    from perl using regexs but I have not had no success so I was hoping
    that I could get a little help on this. I just ordered the regex book
    from oreilly so that hopefully in the future I will not have to ask a
    question like this again.
    The number is 0000000000000111
    I was just wanting to get the last 3 digits from this number to be
    able to get an exact word phrase from my already loaded Hash table
    that I have created for various numbers that I will get the last 3
    numbers from.
    To get the last three characters:

    $ perl -le'my $number = "0000000000000111"; my ( $last_3 ) = $number =~
    /(...)$/; print $last_3'
    111

    Or:

    $ perl -le'my $number = "0000000000000111"; my $last_3 = substr $number,
    -3; print $last_3'
    111

    Of course the regex version will ignore any newline at the end.

    To get the last three numerical digits only:

    $ perl -le'my $number = "0000000000000111"; my ( $last_3 ) = $number =~
    /(\d\d\d)$/; print $last_3'
    111

    Or:

    $ perl -le'my $number = "0000000000000111"; my ( $last_3 ) = $number =~
    /.*(\d\d\d)/; print $last_3'
    111

    \d\d\d could also be written as \d{3}.
    Oh yeah all I was doing was putting this number in a
    file and then passing it to perl on the command line incase that is
    needed.
    I can not think of why that may be needed?

    John
    --
    The programmer is fighting against the two most
    destructive forces in the universe: entropy and
    human stupidity.               -- Damian Conway
    Hello Everyone,

    Thanks for all the help all of them worked just what I needed . Also
    Thanks John for all of the examples thatI could choose from.
  • Shadow52 at Nov 23, 2009 at 10:06 pm

    On Nov 23, 9:22 am, jwkr...@shaw.ca (John W. Krahn) wrote:
    shadow52 wrote:
    Hey everyone, Hello,
    I am trying to get just the last 3 numbers from the following number
    from perl using regexs but I have not had no success so I was hoping
    that I could get a little help on this. I just ordered the regex book
    from oreilly so that hopefully in the future I will not have to ask a
    question like this again.
    The number is 0000000000000111
    I was just wanting to get the last 3 digits from this number to be
    able to get an exact word phrase from my already loaded Hash table
    that I have created for various numbers that I will get the last 3
    numbers from.
    To get the last three characters:

    $ perl -le'my $number = "0000000000000111"; my ( $last_3 ) = $number =~
    /(...)$/; print $last_3'
    111

    Or:

    $ perl -le'my $number = "0000000000000111"; my $last_3 = substr $number,
    -3; print $last_3'
    111

    Of course the regex version will ignore any newline at the end.

    To get the last three numerical digits only:

    $ perl -le'my $number = "0000000000000111"; my ( $last_3 ) = $number =~
    /(\d\d\d)$/; print $last_3'
    111

    Or:

    $ perl -le'my $number = "0000000000000111"; my ( $last_3 ) = $number =~
    /.*(\d\d\d)/; print $last_3'
    111

    \d\d\d could also be written as \d{3}.
    Oh yeah all I was doing was putting this number in a
    file and then passing it to perl on the command line incase that is
    needed.
    I can not think of why that may be needed?

    John
    --
    The programmer is fighting against the two most
    destructive forces in the universe: entropy and
    human stupidity.               -- Damian Conway
    Hello Everyone,

    Thanks for all the help all of them worked just what I needed . Also
    Thanks John for all of the examples thatI could choose from.
  • Gaochong at Nov 24, 2009 at 5:21 am
    But I think substr is better .

    my $str="0000000000000111";
    my $r=substr ($str,-3);
    -----邮件原件-----
    发件人: John W. Krahn
    发送时间: 2009年11月日 23:23
    收件人: Perl Beginners
    主题: Re: Regex to get last 3 digits of a number.

    shadow52 wrote:
    Hey everyone, Hello,
    I am trying to get just the last 3 numbers from the following number
    from perl using regexs but I have not had no success so I was hoping
    that I could get a little help on this. I just ordered the regex book
    from oreilly so that hopefully in the future I will not have to ask a
    question like this again.

    The number is 0000000000000111

    I was just wanting to get the last 3 digits from this number to be
    able to get an exact word phrase from my already loaded Hash table
    that I have created for various numbers that I will get the last 3
    numbers from.
    To get the last three characters:

    $ perl -le'my $number = "0000000000000111"; my ( $last_3 ) = $number =~
    /(...)$/; print $last_3'
    111

    Or:

    $ perl -le'my $number = "0000000000000111"; my $last_3 = substr $number,
    -3; print $last_3'
    111

    Of course the regex version will ignore any newline at the end.

    To get the last three numerical digits only:

    $ perl -le'my $number = "0000000000000111"; my ( $last_3 ) = $number =~
    /(\d\d\d)$/; print $last_3'
    111

    Or:

    $ perl -le'my $number = "0000000000000111"; my ( $last_3 ) = $number =~
    /.*(\d\d\d)/; print $last_3'
    111


    \d\d\d could also be written as \d{3}.

    Oh yeah all I was doing was putting this number in a
    file and then passing it to perl on the command line incase that is
    needed.
    I can not think of why that may be needed?



    John
    --
    The programmer is fighting against the two most
    destructive forces in the universe: entropy and
    human stupidity. -- Damian Conway

    --
    To unsubscribe, e-mail: beginners-unsubscribe@perl.org
    For additional commands, e-mail: beginners-help@perl.org
    http://learn.perl.org/
  • Shawn H Corey at Nov 24, 2009 at 12:25 pm

    gaochong wrote:
    But I think substr is better .

    my $str="0000000000000111";
    my $r=substr ($str,-3);
    TimTowTdi (there is more than one way to do it):

    my $n = "0000000000000111";
    my $last_3_chars = join('',(split(//,$n))[-3..-1]);
    print "$last_3_chars\n";


    --
    Just my 0.00000002 million dollars worth,
    Shawn

    Programming is as much about organization and communication
    as it is about coding.

    I like Perl; it's the only language where you can bless your
    thingy.
  • Yonghua Peng at Nov 24, 2009 at 12:50 pm

    --- On Tue, 24/11/09, Shawn H Corey wrote:

    From: Shawn H Corey <shawnhcorey@gmail.com>
    Subject: Re: 答复: Regex to get last 3 digits of a number.
    To: "gaochong" <zjgaochong@gmail.com>
    Cc: "'John W. Krahn'" <jwkrahn@shaw.ca>, "'Perl Beginners'" <beginners@perl.org>
    Received: Tuesday, 24 November, 2009, 8:26 PM
    gaochong wrote:
    But I think substr is better .

    my $str="0000000000000111";
    my $r=substr ($str,-3);
    TimTowTdi (there is more than one way to do it):

    my $n = "0000000000000111";
    my $last_3_chars = join('',(split(//,$n))[-3..-1]);
    The worst way, :-)
    split is much slower than others.


    __________________________________________________________________________________
    Win 1 of 4 Sony home entertainment packs thanks to Yahoo!7.
    Enter now: http://au.docs.yahoo.com/homepageset/
  • Shawn H Corey at Nov 24, 2009 at 2:13 pm

    Yonghua Peng wrote:

    --- On Tue, 24/11/09, Shawn H Corey wrote:
    From: Shawn H Corey <shawnhcorey@gmail.com>
    Subject: Re: 答复: Regex to get last 3 digits of a number.
    To: "gaochong" <zjgaochong@gmail.com>
    Cc: "'John W. Krahn'" <jwkrahn@shaw.ca>, "'Perl Beginners'" <beginners@perl.org>
    Received: Tuesday, 24 November, 2009, 8:26 PM
    gaochong wrote:
    But I think substr is better .

    my $str="0000000000000111";
    my $r=substr ($str,-3);
    TimTowTdi (there is more than one way to do it):

    my $n = "0000000000000111";
    my $last_3_chars = join('',(split(//,$n))[-3..-1]);
    The worst way, :-)
    split is much slower than others.
    Worst? Hardly. I'm just getting warmed up.

    my $n = "0000000000000111";

    my $last_3_digits = sprintf '%03d', $n - int( $n / 1_000 ) * 1_000;
    print "$last_3_digits\n";

    my $fluff = length( $n ) - 3;
    my $last_3_chars = (unpack "a$fluff a*", $n)[-1];
    print "$last_3_chars\n";


    --
    Just my 0.00000002 million dollars worth,
    Shawn

    Programming is as much about organization and communication
    as it is about coding.

    I like Perl; it's the only language where you can bless your
    thingy.
  • John W. Krahn at Nov 24, 2009 at 3:06 pm

    Shawn H Corey wrote:
    Yonghua Peng wrote:
    --- On Tue, 24/11/09, Shawn H Corey wrote:
    From: Shawn H Corey <shawnhcorey@gmail.com>
    Subject: Re: 答�: Regex to get last 3 digits of a number.
    To: "gaochong" <zjgaochong@gmail.com>
    Cc: "'John W. Krahn'" <jwkrahn@shaw.ca>, "'Perl Beginners'" <beginners@perl.org>
    Received: Tuesday, 24 November, 2009, 8:26 PM
    gaochong wrote:
    But I think substr is better .

    my $str="0000000000000111";
    my $r=substr ($str,-3);
    TimTowTdi (there is more than one way to do it):

    my $n = "0000000000000111";
    my $last_3_chars = join('',(split(//,$n))[-3..-1]);
    The worst way, :-)
    split is much slower than others.
    Worst? Hardly. I'm just getting warmed up.

    my $n = "0000000000000111";

    my $last_3_digits = sprintf '%03d', $n - int( $n / 1_000 ) * 1_000;
    Or:

    my $last_3_digits = sprintf '%03d', $n % 1_000;

    print "$last_3_digits\n";

    my $fluff = length( $n ) - 3;
    my $last_3_chars = (unpack "a$fluff a*", $n)[-1];
    Or:

    my $last_3_chars = unpack "x$fluff a*", $n;

    print "$last_3_chars\n";

    John
    --
    The programmer is fighting against the two most
    destructive forces in the universe: entropy and
    human stupidity. -- Damian Conway

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedNov 23, '09 at 3:27a
activeNov 24, '09 at 3:16p
posts20
users10
websiteperl.org

People

Translate

site design / logo © 2022 Grokbase