Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

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

by Anonymous Monk
on Feb 11, 2014 at 23:19 UTC ( [id://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?

Replies are listed 'Best First'.
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 NetWallah (Canon) 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 choroba (Cardinal) 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 (Saint) 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 MidLifeXis (Monsignor) on Feb 12, 2014 at 16:54 UTC

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

    --MidLifeXis

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
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?Last hourOther CB clients
Other Users?
Others studying the Monastery: (6)
As of 2024-04-19 11:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found