Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^2: How to extract the first elemnet from an integer type value ???

by Velaki (Chaplain)
on Aug 31, 2006 at 17:08 UTC ( [id://570642]=note: print w/replies, xml ) Need Help??


in reply to Re: How to extract the first elemnet from an integer type value ???
in thread How to extract the first elemnet from an integer type value ???

Perhaps you meant this?

print reverse($num) % 10;

Pax vobiscum et gratias vobis ago valeteque,
-v

"Perl. There is no substitute."
  • Comment on Re^2: How to extract the first elemnet from an integer type value ???
  • Download Code

Replies are listed 'Best First'.
Re^3: How to extract the first elemnet from an integer type value ???
by davido (Cardinal) on Aug 31, 2006 at 18:28 UTC

    No, I meant print reverse($num)+0;, which extracts '1' from the integer 100, as the OP asked. He didn't ask about 52, 583, 47, or 32767, he asked about 100. ;)

    Yours works too, of course. The difference being, yours works over any integer, and mine works for the integer 100, or any other integer where the leading digit contains a positive value and the subsequent digits are zero. But both extract 1 from 100.

    He may also have meant all integers, or he may also have meant any integer with one leading significant digit and all trailing digits being zero. Or he may have meant only the integer 100, or maybe an integer beginning with 1 and followed by only zeros, or maybe he meant only positive integers, or maybe something else entirely, but he said an integer, 100. Any other assertion is guesswork.

    Of course we're just having fun. Everyone knows the best way is this:

    print shift @{[(split//,$num)[0-(@{[split//,$num]})]]}

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://570642]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2024-04-26 04:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found