Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Closed Thread
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 5th, 2012, 10:43 AM
JJ_of_c9 JJ_of_c9 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Location: Youngstown, OH, USA
Posts: 5 JJ_of_c9 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 2 m 34 sec
Reputation Power: 0
Filesizes

Hello Dev-Shed Forums:

This is my second week using and studying Perl.

I am "most of the way" there on a script that I wrote to give me filesizes for my backup /mount_dir

It is only my second script.

perl Code:
Original - perl Code
  1. #!/usr/bin/perl -w
  2.  
  3. ##########
  4. #
  5. # Project     : c9backups disk usage Report
  6. # Started     : 11/02/2012 03:17:05 PM
  7. # Author      : JJ of cirrhus9.com
  8. # Description : Read /c9backups and make a Report.
  9. #
  10. ##########
  11.  
  12. @files = </c9backups/c9*/*.gz>;
  13.  foreach $file (@files) {
  14.         $filesize = -s $file;
  15.     if ($filesize > 1024) {
  16.         $filesize=sprintf("%0.2d", ($filesize/1024)); #KB
  17.         print "$file = $filesize kb\n"; } else {
  18.         print "$file = $filesize mb\n";
  19.     }
  20. }
  21.  


but all I manage to get correctly is kb:
Code:
/c9backups/c9bacula/c9bacula-ScriptedDump_11-01-2012.1351828501.tar.gz = 118 kb
/c9backups/c9bacula/c9bacula-ScriptedDump_11-02-2012.1351914901.tar.gz = 118 kb
/c9backups/c9bacula/c9bacula-ScriptedDump_11-03-2012.1352001301.tar.gz = 118 kb
/c9backups/c9bacula/c9bacula-ScriptedDump_11-04-2012.1352091301.tar.gz = 118 kb
/c9backups/c9wiki/c9wiki-ScriptedDump_11-01-2012.1351839301.tar.gz = 22651 kb
/c9backups/c9wiki/c9wiki-ScriptedDump_11-02-2012.1351925701.tar.gz = 22651 kb
/c9backups/c9wiki/c9wiki-ScriptedDump_11-03-2012.1352012101.tar.gz = 22651 kb
/c9backups/c9wiki/c9wiki-ScriptedDump_11-04-2012.1352102101.tar.gz = 22651 kb
/c9backups/c9zabbix/c9zabbix-ScriptedDump_11-01-2012.1351828501.tar.gz = 349132 kb
/c9backups/c9zabbix/c9zabbix-ScriptedDump_11-02-2012.1351914901.tar.gz = 352352 kb
/c9backups/c9zabbix/c9zabbix-ScriptedDump_11-03-2012.1352001301.tar.gz = 355203 kb
/c9backups/c9zabbix/c9zabbix-ScriptedDump_11-04-2012.1352091301.tar.gz = 358551 kb


/c9backups/c9wiki/* and
/c9backups/c9zabbix/* coughing up kb instead of mb.

I borrowed the code from http://forums.devshed.com/showpost.php?p=2264323&postcount=2

I have played with various incarnations of
"$filesize=sprintf("%0.2d", ($filesize/1024)); #MB" but I either got both kb and mb or the current output.

I am not sure where my error is and I need help with this basic task.

Can someone help me out?

Thank you for your time.

I read at http://perl-begin.org/tutorials/bad-elements/#foreach-lines that using foreach is bad form?

but that's all my meager skills can do atm.

Thanks!

Reply With Quote
  #2  
Old November 5th, 2012, 11:52 AM
Axweildr's Avatar
Axweildr Axweildr is offline
'fie' on me, allege-dly
Click here for more information.
 
Join Date: Mar 2003
Location: in da kitchen ...
Posts: 12,874 Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)  Folding Points: 162285 Folding Title: Super Ultimate Folder - Level 1Folding Points: 162285 Folding Title: Super Ultimate Folder - Level 1Folding Points: 162285 Folding Title: Super Ultimate Folder - Level 1Folding Points: 162285 Folding Title: Super Ultimate Folder - Level 1Folding Points: 162285 Folding Title: Super Ultimate Folder - Level 1Folding Points: 162285 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 4 Months 2 Weeks 1 Day 20 h 28 m 56 sec
Reputation Power: 6421
Send a message via Google Talk to Axweildr
Orkut
perl Code:
Original - perl Code
  1. foreach $file (@files) {
  2.     $filesize = -s $file;
  3.     $filesize=sprintf("%0.2d", ($filesize/1024))." kb"; #KB
  4.     if ($filesize > 1024) {
  5.       $filesize=sprintf("%0.2d", ($filesize/1024))." mb" # MB
  6.     }
  7.     print $file."\t\t\t".$filesize."\n";
  8. }
__________________
--Ax
without exception, there is no rule ...
Handmade Irish Jewellery
Targeted Advertising Cookie Optout (TACO) extension for Firefox
The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones


09 F9 11 02
9D 74 E3 5B
D8 41 56 C5
63 56 88 C0
Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
-- Jamie Zawinski
Detavil - the devil is in the detail, allegedly, and I use the term advisedly, allegedly ... oh, no, wait I did ...
BIT COINS ANYONE

Reply With Quote
  #3  
Old November 5th, 2012, 11:53 AM
Laurent_R Laurent_R is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2012
Posts: 506 Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 4 Days 19 h 5 m 2 sec
Reputation Power: 385
It seems to me that your code lacks a condition to check whether the file is more than 1024 kilobytes and also lacks the division by 1024 in such a case, so that when you write so many megabytes, you are actually printing the number of bytes.

Reply With Quote
  #4  
Old November 5th, 2012, 12:45 PM
JJ_of_c9 JJ_of_c9 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Location: Youngstown, OH, USA
Posts: 5 JJ_of_c9 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 2 m 34 sec
Reputation Power: 0
Thank you both.
Axweildr's snippet gets me further.
I now have this output using it:
Argument "118 kb" isn't numeric in numeric gt (>) at jj9.pl line 15

partial output # showing [km]b identifiers
Code:
/c9backups/c9bacula/c9bacula-ScriptedDump_11-04-2012.1352091301.tar.gz	118 kb
Argument "22651 kb" isn't numeric in numeric gt (>) at jj9.pl line 15.
/c9backups/c9wiki/c9wiki-ScriptedDump_11-01-2012.1351839301.tar.gz	22 mb
Argument "22651 kb" isn't numeric in numeric gt (>) at jj9.pl line 15.
/c9backups/c9wiki/c9wiki-ScriptedDump_11-02-2012.1351925701.tar.gz	22 mb
Argument "22651 kb" isn't numeric in numeric gt (>) at jj9.pl line 15.
/c9backups/c9wiki/c9wiki-ScriptedDump_11-03-2012.1352012101.tar.gz	22 mb
Argument "22651 kb" isn't numeric in numeric gt (>) at jj9.pl line 15.
/c9backups/c9wiki/c9wiki-ScriptedDump_11-04-2012.1352102101.tar.gz	22 mb
Argument "349132 kb" isn't numeric in numeric gt (>) at jj9.pl line 15.
/c9backups/c9zabbix/c9zabbix-ScriptedDump_11-01-2012.1351828501.tar.gz	340 mb


I could guess what I need to do all day long, but I prefer not to in this case.
But my gut and screen both scream at me the kb in the output is what is causing the "error" and I am not sure how to proceed to correct that.


Thank you both for your time,

Edit0:
I removed the -w on the shebang line and now it's "better".
It may not be 'correct' but I still await a solution with better 'form'.

Thanks!

Reply With Quote
  #5  
Old November 5th, 2012, 01:57 PM
Laurent_R Laurent_R is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2012
Posts: 506 Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 4 Days 19 h 5 m 2 sec
Reputation Power: 385
Don't remove the -w from the shebang, or, rather, do it, but add a "use warnings;" line. And correct the reason for the warnings.

The point is that you should store the number of bytes, kB, etc. as a pure number, and, on the other side, without the string " kB") and add the " kB" thing when you print. But if you are trying to do a numeric comparison (< or >) on "22651 kb" and some other value), the program is telling quite rightly that it does not make much sense.

Reply With Quote
  #6  
Old November 6th, 2012, 01:35 PM
JJ_of_c9 JJ_of_c9 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Location: Youngstown, OH, USA
Posts: 5 JJ_of_c9 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 2 m 34 sec
Reputation Power: 0
Thanks Laurent_R:

I gathered this also.
Page 27 (6th Ed.) of the Llama book specifically covers this situation, but does specifically say how to deal with it. So I read on and look for a solution.

My code.fu tells me that the instruction I need to work on is "$filesize = -s $file;" or manipulate that output just after|below that line, or...something along those lines.

I'll get there!

Thanks for all your help.

Reply With Quote
  #7  
Old November 6th, 2012, 02:18 PM
FishMonger FishMonger is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Apr 2009
Posts: 1,645 FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 1 Day 22 h 51 m 15 sec
Reputation Power: 1170
This is how I'd write that loop.
Code:
foreach my $file ( @files ) {
    my $filesize = -s $file;
    my ($div, $byte) = $filesize < 1024 ? (1, 'B')
                                        : $filesize < 1024**2 ? (1024, 'KB')
                                        : $filesize < 1024**3 ? (1024**2, 'MB')
                                        : (1024**3, 'GB');
    printf("$file = %0.2d $byte\n", $filesize/$div);
}
Comments on this post
JJ_of_c9 agrees: excellent solution given.

Reply With Quote
  #8  
Old November 6th, 2012, 02:59 PM
JJ_of_c9 JJ_of_c9 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Location: Youngstown, OH, USA
Posts: 5 JJ_of_c9 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 2 m 34 sec
Reputation Power: 0
Thanks FishMonger!

The code is "perfect" but what I need to know is if my logic was "on target" or not in my last reply/post?

I have been a shell script/bash coding enthusiast for years, and I took on the discipline of Perl with the hopes that my experience in the former could apply to the latter (being Perl).

I understand also if my "logic" doesn't register with anyone as I am an odd duck. Old(er) and rather particular in my methods. It is also known to me that there are many, many ways to skin a particular cat in Perl.

I appreciate your time.

Thank you,
JJ

Reply With Quote
  #9  
Old November 6th, 2012, 03:26 PM
FishMonger FishMonger is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Apr 2009
Posts: 1,645 FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 1 Day 22 h 51 m 15 sec
Reputation Power: 1170
The logic used in your opening post is wrong. If you're going to use > to test for greater-than, then you need to start at the upper end of the scale. Your logic states that all filesizes less than 1024 (1KB) are MB, which clearly is wrong.

Axweildr's logic is a little better but incomplete and creates a bug by turning the filesize into a string and then attempts to use that string in a numerical calculation/test.

Reply With Quote
  #10  
Old November 6th, 2012, 03:32 PM
JJ_of_c9 JJ_of_c9 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Location: Youngstown, OH, USA
Posts: 5 JJ_of_c9 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 2 m 34 sec
Reputation Power: 0
Thank you for your response.

I appreciate your time.

Reply With Quote
Closed Thread

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > Filesizes

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap