Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^2: sumof - attempting to sum a column from each file

by ricky5ive (Initiate)
on Sep 14, 2012 at 21:12 UTC ( #993798=note: print w/ replies, xml ) Need Help??


in reply to Re: sumof - attempting to sum a column from each file
in thread sumof - attempting to sum a column from each file

It's actually output from a Netapp. I like the idea of simpler ... helps to learn the ways of the jedi.

<df -k> Filesystem total used avail capacity Mounted + on /vol/vol0/ 142606336KB 5274244KB 137332092KB 4% /vol/v +ol0/ /vol/vol0/.snapshot 35651584KB 923524KB 34728060KB 3% /vol/vo +l0/.snapshot /vol/vobstore/ 734003200KB 6335232KB 727667968KB 1% /vol/ +vobstore/ /vol/vobstore/.snapshot 183500800KB 260200KB 183240600KB 0% / +vol/vobstore/.snapshot /vol/ccase/ 125829120KB 21814420KB 104014700KB 17% /vol/ +ccase/ /vol/ccase/.snapshot 31457280KB 90572KB 31366708KB 0% /vol/c +case/.snapshot /vol/viewstore/ 943718400KB 6492604KB 937225796KB 1% /vol/ +viewstore/ /vol/viewstore/.snapshot 235929600KB 137500KB 235792100KB 0% +/vol/viewstore/.snapshot /vol/viewstore_win/ 67108864KB 480KB 67108384KB 0% /vol/vi +ewstore_win/ /vol/viewstore_win/.snapshot 16777216KB 2372KB 16774844KB 0% + /vol/viewstore_win/.snapshot </df -k>


Comment on Re^2: sumof - attempting to sum a column from each file
Download Code
Re^3: sumof - attempting to sum a column from each file
by CountZero (Chancellor) on Sep 15, 2012 at 07:14 UTC
    Try this for your summing code:
    use Modern::Perl; my $sum_of_used; { no warnings qw/numeric uninitialized/; $sum_of_used += (split /\s+/)[2] while <DATA>; } say $sum_of_used; __DATA__ <df -k> Filesystem total used avail capacity Mounted + on /vol/vol0/ 142606336KB 5274244KB 137332092KB 4% /vol/v +ol0/ /vol/vol0/.snapshot 35651584KB 923524KB 34728060KB 3% /vol/vo +l0/.snapshot /vol/vobstore/ 734003200KB 6335232KB 727667968KB 1% /vol/ +vobstore/ /vol/vobstore/.snapshot 183500800KB 260200KB 183240600KB 0% / +vol/vobstore/.snapshot /vol/ccase/ 125829120KB 21814420KB 104014700KB 17% /vol/ +ccase/ /vol/ccase/.snapshot 31457280KB 90572KB 31366708KB 0% /vol/c +case/.snapshot /vol/viewstore/ 943718400KB 6492604KB 937225796KB 1% /vol/ +viewstore/ /vol/viewstore/.snapshot 235929600KB 137500KB 235792100KB 0% +/vol/viewstore/.snapshot /vol/viewstore_win/ 67108864KB 480KB 67108384KB 0% /vol/vi +ewstore_win/ /vol/viewstore_win/.snapshot 16777216KB 2372KB 16774844KB 0% + /vol/viewstore_win/.snapshot </df -k>
    It can be so simple because Perl knows how to make a number out of "almost" numbers. In other words, Perl simply disregards the 'KB' which is attached to the number. Everything which does not contain numbers will become 0, such as the header line. A perfect example of Perlish "Do What I Mean".

    Of course I had to switch of the "not numeric" and "not initialized" warnings, but that is OK if you know what you are doing.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
Re^3: sumof - attempting to sum a column from each file
by CountZero (Chancellor) on Sep 15, 2012 at 07:31 UTC
    And your whole program can be as short as this:
    use Modern::Perl; my $sum_of_used; @ARGV = <C:/Users/user/Desktop/DOwork/filez/nabillingscript/09_14_2012 +/nas/*>; { no warnings qw/numeric uninitialized/; $sum_of_used += (split /\s+/)[2] while <ARGV>; } say $sum_of_used;
    It uses the magic attached to the ARGV filehandle which automatically iterates over all lines of all filenames in @ARGV.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics

      Very interesting. So you're using a subroutine to calculate the sum?

        A subroutine? Not at all.

        I have put the code inside a block (i.e. the curly braces) to limit the scope of the no warnings qw/numeric uninitialized/ pragma.

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

        My blog: Imperial Deltronics

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (12)
As of 2014-04-21 15:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (495 votes), past polls