Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Manipulating data from a file

by tobyink (Abbot)
on Dec 06, 2012 at 09:01 UTC ( #1007502=note: print w/ replies, xml ) Need Help??


in reply to Manipulating data from a file

The maths part isn't especially tricky...

use 5.010; use strict; use warnings; # This expression is used to test if a string consists of a single # digit. We use it a couple of different places, so we'll just define # it once as a constant. # use constant DIGIT => qr/^[0-9]$/; # Let's assume you already have the date in an all numeric format. # say digit_sum('01-06-1980'); # This is the function which adds digits # sub digit_sum { my $string = shift; # First, 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'


Comment on Re: Manipulating data from a file
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (13)
As of 2015-07-02 08:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (31 votes), past polls