Code Comments
Programming Forum and web based access to our favorite programming groups.Hi, How do I extract the last digit of a number? example I only want the digit 9 from the number 19. my $number = 19;
Post Follow-up to this messageitshardtogetone@hotmail.com wrote: > > How do I extract the last digit of a number? example I only want the digit 9 from the number 19. > > my $number = 19; There are a few ways. my $lastdigit = substr $number, -1; gives you the last character in the string my $lastdigit = $number % 10; gives you the remainder when $number is divided by 10 my ($lastdigit) = $number =~ /.*([0-9])/; gives you the last decimal digit (even if there are non-decimals after it). HTH, Rob
Post Follow-up to this messageitshardtogetone@hotmail.com wrote: > Hi, Hello, > How do I extract the last digit of a number? example I only want the digit 9 from the number 19. > > my $number = 19; my $last_digit = chop $number; John -- Perl isn't a toolbox, but a small machine shop where you can special-order certain sorts of tools at low cost and in short order. -- Larry Wall
Post Follow-up to this messageJohn W. Krahn wrote: > itshardtogetone@hotmail.com wrote: > > Hello, > > > my $last_digit = chop $number; Yes, although it's important to note that that method removes the character from $number as well, leaving it equal to '1'. Rob
Post Follow-up to this messageT24gVHVlLCBBcHIgMSwgMjAwOCBhdCAxMTowOSBB TSwgUm9iIERpeG9uIDxyb2IuZGl4b25AZ214 LmNvbT4gd3JvdGU6Cgpbc25pcF0KCj4KPiAgIG15 ICgkbGFzdGRpZ2l0KSA9ICRudW1iZXIgPX4g Ly4qKFswLTldKS87Cj4KPiAgZ2l2ZXMgeW91IHRo ZSBsYXN0IGRlY2ltYWwgZGlnaXQgKGV2ZW4g aWYgdGhlcmUgYXJlIG5vbi1kZWNpbWFscyBhZnRl cgo+ICBpdCkuCj4KCkJldHRlciB5ZXQ6Cgog ICAgbXkgKCRsYXN0KSA9ICRudW1iZXIgPX4gLy4q KFxkKS87CgpMZXQgUGVybCB3b3JyeSBhYm91 dCB3aGF0IGlzIGFuZCBpc24ndCBhIGRpZ2l0LgoK QmVzdCwKCi0tIGpheQotLS0tLS0tLS0tLS0t LS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0t LS0tLS0tLQpUaGlzIGVtYWlsIGFuZCBhdHRh Y2htZW50KHMpOiBbIF0gYmxvZ2FibGU7IFsgeCBd IGFzayBmaXJzdDsgWyBdCnByaXZhdGUgYW5k IGNvbmZpZGVudGlhbAoKZGFnZ2VycXVpbGwgW2F0 XSBnbWFpbCBbZG90XSBjb20KaHR0cDovL3d3 dy50dWF3LmNvbSBodHRwOi8vd3d3LmRvd25sb2Fk c3F1YWQuY29tIGh0dHA6Ly93d3cuZW5nYXRp a2kub3JnCgp2YWx1ZXMgb2Yg4iB3aWxsIGdpdmUg cmlzZSB0byBkb20hCg==
Post Follow-up to this messageIs there some way to convert it (a string/scalar) to an array?
Post Follow-up to this messageyitzle wrote: > Is there some way to convert it (a string/scalar) to an array? Is this what you mean? my $string = 'ABCDEF'; my @string = split '', $string; Rob
Post Follow-up to this messageOn Tue, Apr 1, 2008 at 4:03 PM, Rob Dixon <rob.dixon@gmx.com> wrote: > yitzle wrote: > > Is this what you mean? > > my $string = 'ABCDEF'; > my @string = split '', $string; > > > > Rob I suppose. That can be used to extract the last digit. I was actually thinking of, um, is there some function that can be used to convert a string to a list of binary/hex values? Or am I imagining it?
Post Follow-up to this message2008/4/1 Jay Savage <daggerquill@gmail.com>:
snip
> my ($last) = $number =~ /.*(\d)/;
>
> Let Perl worry about what is and isn't a digit.
snip
Unfortunately, with the rise of UNICODE, \d is no longer what one
expects* ([0-9]). It now includes all characters marked as digits in
UNICODE. This includes characters like "\x{1813}" (MONGOLIAN DIGIT
THREE). The \w character class also no longer matches [a-zA-Z0-9_],
but instead matches any character marked as a word character by
UNICODE; however, this is much less of a problem since, unlike digits,
a character is still a character (try adding "\x{1813}" + 1).
* note, you can get the old behavior back by using the bytes pragma.
--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.
Post Follow-up to this messageOn Tue, Apr 1, 2008 at 5:17 PM, yitzle <yitzle@users.sourceforge.net> wrote: > On Tue, Apr 1, 2008 at 4:03 PM, Rob Dixon <rob.dixon@gmx.com> wrote: > > I suppose. That can be used to extract the last digit. > I was actually thinking of, um, is there some function that can be > used to convert a string to a list of binary/hex values? Or am I > imagining it? snip Are you thinking of unpack*? perl -e 'print map "[$_]\n", unpack "c*", "abcdef"' * http://perldoc.perl.org/functions/unpack.html -- Chas. Owens wonkden.net The most important skill a programmer can have is the ability to read.
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.