Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

how to extract the last digit
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;


Report this thread to moderator Post Follow-up to this message
Old Post
itshardtogetone@hotmail.com
04-02-08 12:05 AM


Re: how to extract the last digit
itshardtogetone@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

Report this thread to moderator Post Follow-up to this message
Old Post
Rob Dixon
04-02-08 12:05 AM


Re: how to extract the last digit
itshardtogetone@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

Report this thread to moderator Post Follow-up to this message
Old Post
John W. Krahn
04-02-08 12:05 AM


Re: how to extract the last digit
John 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


Report this thread to moderator Post Follow-up to this message
Old Post
Rob Dixon
04-02-08 12:05 AM


Re: how to extract the last digit
T24gVHVlLCBBcHIgMSwgMjAwOCBhdCAxMTowOSBB
TSwgUm9iIERpeG9uIDxyb2IuZGl4b25AZ214
 LmNvbT4gd3JvdGU6Cgpbc25pcF0KCj4KPiAgIG15
ICgkbGFzdGRpZ2l0KSA9ICRudW1iZXIgPX4g
 Ly4qKFswLTldKS87Cj4KPiAgZ2l2ZXMgeW91IHRo
ZSBsYXN0IGRlY2ltYWwgZGlnaXQgKGV2ZW4g
 aWYgdGhlcmUgYXJlIG5vbi1kZWNpbWFscyBhZnRl
cgo+ICBpdCkuCj4KCkJldHRlciB5ZXQ6Cgog
 ICAgbXkgKCRsYXN0KSA9ICRudW1iZXIgPX4gLy4q
KFxkKS87CgpMZXQgUGVybCB3b3JyeSBhYm91
 dCB3aGF0IGlzIGFuZCBpc24ndCBhIGRpZ2l0LgoK
QmVzdCwKCi0tIGpheQotLS0tLS0tLS0tLS0t
 LS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0t
LS0tLS0tLQpUaGlzIGVtYWlsIGFuZCBhdHRh
 Y2htZW50KHMpOiBbIF0gYmxvZ2FibGU7IFsgeCBd
IGFzayBmaXJzdDsgWyBdCnByaXZhdGUgYW5k
 IGNvbmZpZGVudGlhbAoKZGFnZ2VycXVpbGwgW2F0
XSBnbWFpbCBbZG90XSBjb20KaHR0cDovL3d3
 dy50dWF3LmNvbSBodHRwOi8vd3d3LmRvd25sb2Fk
c3F1YWQuY29tIGh0dHA6Ly93d3cuZW5nYXRp
 a2kub3JnCgp2YWx1ZXMgb2Yg4iB3aWxsIGdpdmUg
cmlzZSB0byBkb20hCg==

Report this thread to moderator Post Follow-up to this message
Old Post
Jay Savage
04-02-08 12:05 AM


Re: how to extract the last digit
Is there some way to convert it (a string/scalar) to an array?

Report this thread to moderator Post Follow-up to this message
Old Post
Yitzle
04-02-08 12:05 AM


Re: how to extract the last digit
yitzle 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

Report this thread to moderator Post Follow-up to this message
Old Post
Rob Dixon
04-02-08 12:05 AM


Re: how to extract the last digit
On 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?

Report this thread to moderator Post Follow-up to this message
Old Post
Yitzle
04-02-08 12:05 AM


Re: how to extract the last digit
2008/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.

Report this thread to moderator Post Follow-up to this message
Old Post
Chas. Owens
04-02-08 12:05 AM


Re: how to extract the last digit
On 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.

Report this thread to moderator Post Follow-up to this message
Old Post
Chas. Owens
04-02-08 12:05 AM


Sponsored Links




Last Thread Next Thread Next
Pages (2): [1] 2 »
Search this forum -> 
Post New Thread

PERL Beginners archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 09:38 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.