Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

How can I write this in a "simpler" way?

by Anonymous Monk
on Feb 11, 2014 at 23:19 UTC ( #1074534=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello dear Monks!
I am again stuck because I am supposed to answer the following question without using the <> operator.
The question is, given a file with numbers, and using the while loop, how can you sum up the values that are in the file?
Of course the easy way would be something like:
$count=0; while(<>) { $mynumber = $_; chomp $mynumber; $count +=$mynumber; } print $count;

Any more "primitive" way for this?

Comment on How can I write this in a "simpler" way?
Download Code
Re: How can I write this in a "simpler" way?
by Anonymous Monk on Feb 11, 2014 at 23:22 UTC
    I just found this:
    while(defined $line=<STDIN>) { so something }

    Is this the way to go?
      It still uses the <> operator, with STDIN as the operand.
      لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
        Yeah, his "problem" is not to use the <> like the general way, <STDIN> is acceptable.
Re: How can I write this in a "simpler" way?
by Anonymous Monk on Feb 11, 2014 at 23:23 UTC
    you fail the test
Re: How can I write this in a "simpler" way?
by choroba (Canon) on Feb 11, 2014 at 23:25 UTC
    Are you allowed to use the readline function?
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      I think he wants us to use the "defined" function, "readline" will be taught in another day :P
Re: How can I write this in a "simpler" way?
by LanX (Canon) on Feb 11, 2014 at 23:33 UTC
    You need to open a "file" and use a filehandle what you show applies to STDIN only.

    Cheers Rolf

    ( addicted to the Perl Programming Language)

      Yeah, because technically we haven't reached the files yet, we are supposed to "feed" the input of the file with re-direction to the program, like:
      cat MYFILE | program.pl

      so, in that case, I guess what I wrote with defined is the correct way?
Re: How can I write this in a "simpler" way?
by NetWallah (Abbot) on Feb 12, 2014 at 00:10 UTC
    How about:
    perl -nE "$s+=$_}{say qq|total $s|" myfile.txt
    (Use single-quotes for Linux).

            What is the sound of Perl? Is it not the sound of a wall that people have stopped banging their heads against?
                  -Larry Wall, 1992

Re: How can I write this in a "simpler" way?
by thezip (Vicar) on Feb 12, 2014 at 00:15 UTC

    YAWTDI...

    #!/usr/bin/perl use strict; use warnings; use Tie::File; my $filename = 'data.txt'; tie(my @array, 'Tie::File', $filename) or die "Could not open $filenam +e for reading"; my $total_lines = 0; for (@array) { chomp; $total_lines += $_; } printf "There are %d lines in %s\n", $total_lines, $filename;

    *My* tenacity goes to eleven...
Re: How can I write this in a "simpler" way?
by MidLifeXis (Monsignor) on Feb 12, 2014 at 16:54 UTC

    perl -ne '$sum += $_; END { print $sum }'

    --MidLifeXis

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2015-07-07 05:58 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 (87 votes), past polls