Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Why is the List upload with STDIN from a text file giving wrong result for mean?

by AnomalousMonk (Abbot)
on Nov 03, 2012 at 03:15 UTC ( #1002071=note: print w/ replies, xml ) Need Help??


in reply to Why is the List upload with STDIN from a text file giving wrong result for mean?

choroba has presented an approach using split and some post-processing of the resultant sub-strings. Here is a regex-based 'extractive' approach that goes after the digits of interest directly and may make it easier to deal with 'messy' strings. See perlre, perlrequick, perlretut.

>perl -wMstrict -le "my $string = qq{(10 ,12, 14 , 16,18)\n}; print qq{string is [[$string]]}; ;; my @array = $string =~ m{ \d+ }xmsg; printf qq{array (@array) has %d elements \n}, scalar @array; ;; my $sum = 0; $sum += $_ for @array; print qq{sum of array is $sum}; ;; my $mean = $sum / @array; print qq{mean of array is $mean}; " string is [[(10 ,12, 14 , 16,18) ]] array (10 12 14 16 18) has 5 elements sum of array is 70 mean of array is 14


Comment on Re: Why is the List upload with STDIN from a text file giving wrong result for mean?
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (11)
As of 2015-07-30 15:06 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 (271 votes), past polls