Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^2: How to split an integer??

by nvivek (Vicar)
on Dec 06, 2012 at 10:32 UTC ( #1007521=note: print w/replies, xml ) Need Help??


in reply to Re: How to split an integer??
in thread How to split an integer??

We can also do it in simple manner by using numeric operator for comparison without having regular expression used in the mentioned thread.

Replies are listed 'Best First'.
Re^3: How to split an integer??
by tobyink (Abbot) on Dec 06, 2012 at 10:39 UTC

    I prefer the regular expression to numeric comparison, because...

    use strict; my $var = "xyz"; print "it's a single-digit number!!\n" if $var < 10;

    Is "xyz" really a single digit number?

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

      But my qn is i have a variable say $a= 23 , now i need to add it as 2+3 which should give me ans 5 ... hw to do this ???

      23 s stored as a single byte hence i cant split. But in words we can split character by character of a word in an array

      my @a = qw (data); foreach $aa(@a){ print $aa."\n"; }

      the above code will not work the sane for integers

        "23 s stored as a single byte hence i cant split."

        Byte?! You're thinking in C terms! Go wash your mouth out with soap right now! ;-)

        In Perl, strings and integers are both represented by the same data type - scalars. (Internally Perl represents scalars as a C struct which has separate string and integer slots. But you don't normally need to worry about such concerns.) If you have a variable that contains an integer value, such as 23, and pass it to something that expects a string, like split, that integer will be silently converted to a string.

        Here's exactly the same function I posted before, only now I'm using the integer 23 as the input...

        use 5.010; use strict; use warnings; # 23 is an integer. # say digit_sum(23); # This expression is used to test that a string consists of a single # digit. We use it a couple of different places, so we'll get define # it once here. # use constant DIGIT => qr/^[0-9]$/; # This is the function which adds digits # sub digit_sum { my $string = shift; # Otherwise, split into digits. my @digits = grep { $_ =~ DIGIT } # keep only the numeric characters split '', $string; # split into characters # Add the digits together. my $sum = 0; $sum += $_ for @digits; # Handle the trivial case. # If $sum is just a single digit, return it as-is. return $sum if $sum =~ DIGIT; # Otherwise, recurse. return digit_sum($sum); }
        perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
        my $number = 23; my @bits = split //, $number; my $total; for my $bit (@bits) { $total += $bit; } print "Number=$number Total=$total\n";
        For splitting integer value, need to specify pattern explicitly as null string as follows.
        # $number contains numerical value which is greater than 9 split('', $number);

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1007521]
help
Chatterbox?
[makita]: sign_types parameter in XML::Compile::WSS ::Signature Does have anybody experience how to use it?
[makita]: Need to sign more elements but all types I put in array are ignored. And is always signed only the body
[Discipulus]: no makita sorry. i see in the synopsis of the module: "WARNING: Only limited real-life experience" might be better compose a SOPW with some code example and data
[Discipulus]: ..or inspect the source code of the module
[makita]: I am doing this:
[makita]: my $sig = $wss->signature( schema => $wsdl, token => $token, sign_types=>'wsa: Action','wsa:To', 'wsu:Timestamp', 'wsa:MessageID',' SOAP-ENV:Body', signer=>DSIG_RSA_S HA1, public_key => $cert,
[Corion]: Oof, I haven't done much with signing SOAP requests. markov is usually somebody good to ask via mail ;)
[makita]: I need to create a SOAP call where are signed more types not only the body
[makita]: I am inspecting it second day :)
[makita]: Thanks

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (11)
As of 2017-03-23 08:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (285 votes). Check out past polls.