Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

How to split an integer??

by nithins (Sexton)
on Dec 06, 2012 at 10:10 UTC ( #1007508=perlquestion: print w/ replies, xml ) Need Help??
nithins has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks, please help me out with this qn. I have a array which contains integer value . if the integer is greater than 9 the i need to split it and add it for ex: '23' i need to split it 2 & 3 and add i,e 2+3=5

can somebody help me how to split an array which contain int > 9

this is the code which i tried

#!/usr/bin/perl use strict; use warnings; my $inp; print "Enter the input file\n"; $inp = <STDIN>; chomp ($inp); my (@date,@mn,@year) ; my $total =0; if( $inp=~/(\d+)\-(\d+)\-(\d+)/) { foreach my $a ($1) { #tried to split here dint worked @date =$1; } @mn =$2; @year =$3; print "@date,@mn,@year\n"; } print scalar(@date); print length(@date);

Comment on How to split an integer??
Download Code
Re: How to split an integer??
by tobyink (Abbot) on Dec 06, 2012 at 10:26 UTC
      We can also do it in simple manner by using numeric operator for comparison without having regular expression used in the mentioned thread.

        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'

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1007508]
Approved by nemesdani
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (11)
As of 2014-07-30 13:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (234 votes), past polls