Home > Archive > PERL Beginners > January 2007 > Reversing Digits
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Dharshana Eswaran 2007-01-09, 4:01 am |
| Hi all,
I was trying to reverse the digits.
For eg: if i have 50, i wanted to print 05
Can anyone tell me a simple way?
Thanks and Regards,
Dharshana
| |
| usenet@DavidFilmer.com 2007-01-09, 4:01 am |
| Dharshana Eswaran wrote:
> For eg: if i have 50, i wanted to print 05
You can use the reverse() function, but you will need to force it to
evaluate your expression in scalar context (it will try to use list
context by default); ie:
my $digits = 50;
print scalar reverse $digits;
--
David Filmer (http://DavidFilmer.com)
| |
| Deepak Barua 2007-01-09, 4:01 am |
| Basically you can use the divide modulus % and then eliminate the
digit using normal divide...
but then the er way is to convert to BCD and then extract nibble
by nibble...
Regards
Deepak
On 1/9/07, Dharshana Eswaran <dharshana.ve@gmail.com> wrote:
> Hi all,
>
> I was trying to reverse the digits.
>
> For eg: if i have 50, i wanted to print 05
>
> Can anyone tell me a simple way?
>
> Thanks and Regards,
> Dharshana
>
>
--
Code Code Code Away
| |
| John W. Krahn 2007-01-09, 4:01 am |
| Dharshana Eswaran wrote:
> Hi all,
Hello,
> I was trying to reverse the digits.
>
> For eg: if i have 50, i wanted to print 05
>
> Can anyone tell me a simple way?
$ perl -le'my $digits = "05"; print for $digits, scalar reverse $digits'
05
50
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
| |
| Paul Lalli 2007-01-09, 7:59 am |
| usenet@DavidFilmer.com wrote:
> Dharshana Eswaran wrote:
>
> You can use the reverse() function, but you will need to force it to
> evaluate your expression in scalar context (it will try to use list
> context by default); ie:
>
> my $digits = 50;
> print scalar reverse $digits;
There is no such thing as a "default" context. Scalar or List context
is imposed by the expression in which the funciton is evaluated.
print() takes a list. Therefore if reverse(...) is given as an
argument to print(), reverse() is called in list context. reverse() is
called in scalar context anywhere Perl is expecting a scalar:
my $x = reverse($digits);
print substr(reverse($digits), 0, 5);
#etc
Paul Lalli
|
|
|
|
|