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

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

by supriyoch_2008 (Scribe)
on Nov 02, 2012 at 11:22 UTC ( #1001947=perlquestion: print w/ replies, xml ) Need Help??
supriyoch_2008 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perl Monks,

My interest is to find the mean from a LIST (10.12,14,16,18). The list is to uploaded from a text file (x.txt) with STDIN. So, I have written the following script i.e. mean.pl and the text file x.txt. But I am getting wrong results for number of elements (i.e.17) and mean (i.e. 0). The correct number of elements and mean should be 5 and 14, respectively. Can any perl monk help in this matter? I think my question might be a very silly one to the Perl Monks.

The perl script mean.pl goes like:

#!/usr/bin/perl use warnings; print"\n Enter the Filename (x.txt) with Data in List context: "; $filename = <STDIN>; chomp$filename; # open the file, or exit unless ( open(FILE, $filename) ) { print "Cannot open file \"$filename\"\n\n"; exit; } $list = <FILE>; print"\n List= $list\n"; @array=split('',$list); # To convert scalar to array print"\n Array From the List= @array\n"; $N_elements=@array; # To count number of elements in array print"\n Number of Elements= $N_elements\n"; # Sum of all elements in array: $sum=0; $sum=eval join '+',@array; # Mean of the list: $mean=$sum/$N_elements; # Line 21 print"\n Mean of the list= $mean\n"; exit;

Here goes the text file x.txt:

(10,12,14,16,18);

The cmd has shown wrong results:

Enter the Filename (x.txt) with Data in List context: x.txt List= (10,12,14,16,18); Array From the List= ( 1 0 , 1 2 , 1 4 , 1 6 , 1 8 ) ; Number= 17 Use of uninitialized value $sum in division (/) at C:\Users\x\Desktop\ +mean.pl line 21, <FILE> line 1. Mean of the list= 0

The correct result should look like:

Enter the Filename (x.txt) with Data in List context: x.txt List= (10,12,14,16,18); Array From the List= ??? Number of Elements= 5 Mean of the list= 14

Comment on Why is the List upload with STDIN from a text file giving wrong result for mean?
Select or Download Code
Re: Why is the List upload with STDIN from a text file giving wrong result for mean?
by Corion (Pope) on Nov 02, 2012 at 11:25 UTC

    Have you looked at the contents of @array?

    Maybe it helps you if you explain what @array=split('',$list); is supposed to do, and compare that with the documentation of split.

      Corion

      Thanks

Re: Why is the List upload with STDIN from a text file giving wrong result for mean?
by choroba (Abbot) on Nov 02, 2012 at 11:26 UTC
    split('', $list) will split the list to characters. You want to split the list on commas:
    split /,/, $list
    Also, if the parentheses are part of the input, you need to get rid of them. You can achieve that by
    $list = s/[()]//g;
    after you read the input.
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      Choroba

      Thank you very much for the prompt reply.

      Regards

Re: Why is the List upload with STDIN from a text file giving wrong result for mean?
by roboticus (Canon) on Nov 02, 2012 at 13:12 UTC

    supriyoch_2008:

    In addition to the other fine advice you've received, I'd suggest you change this:

    unless ( open(FILE, $filename) ) { print "Cannot open file \"$filename\"\n\n"; exit; }

    to this:

    open my $FILE, '<', $filename or die "Cannot open file '$filename': $!\n";

    It's a much more common usage. I'd also change this:

    # Sum of all elements in array: $sum=0; $sum=eval join '+',@array;

    to this:

    # Sum of all elements in array: $sum=0; $sum+=$_ for @array;

    That way, you can avoid a potentially scary string eval. Since you're getting the data for your string from an external source, it's possible that someone might create a string that would let you compromise your system.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      Roboticus

      Thank you very much for your constructive and useful suggestions. I shall try the codes given by you in my script.

      Regards

Re: Why is the List upload with STDIN from a text file giving wrong result for mean?
by AnomalousMonk (Monsignor) on Nov 03, 2012 at 03:15 UTC

    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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2014-08-21 00:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (126 votes), past polls