CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
here is a tricky one :~

 



gerble1000
User

Apr 28, 2012, 2:58 PM

Post #1 of 9 (7891 views)
here is a tricky one :~ Can't Post

i need to be able to find the amount of lines in a txt file then devide it by 2, then devide that by the number of days left in the month.
and output that value to output.txt

any clue :)


FishMonger
Veteran / Moderator

Apr 28, 2012, 4:31 PM

Post #2 of 9 (7854 views)
Re: [gerble1000] here is a tricky one :~ [In reply to] Can't Post

What have you tried?

What part of the process do you not know how to do or is giving you trouble?

Use the Date::Calc module to find the number of days in the month.


(This post was edited by FishMonger on Apr 28, 2012, 4:32 PM)


gerble1000
User

Apr 29, 2012, 2:08 AM

Post #3 of 9 (7633 views)
Re: [FishMonger] here is a tricky one :~ [In reply to] Can't Post

i can get the amount of lines from a text file and thats ll so far


gerble1000
User

Apr 29, 2012, 2:34 AM

Post #4 of 9 (7621 views)
Re: [gerble1000] here is a tricky one :~ [In reply to] Can't Post

i think this devides my variable by 2


Code
my $txtnum = 3000; 
$txtnum \= 2;



(This post was edited by gerble1000 on Apr 29, 2012, 2:34 AM)


gerble1000
User

Apr 29, 2012, 3:59 AM

Post #5 of 9 (7578 views)
Re: [gerble1000] here is a tricky one :~ [In reply to] Can't Post

ok i have figured some out but i am stuck on finding the amount of days in the current month


FishMonger
Veteran / Moderator

Apr 29, 2012, 7:04 AM

Post #6 of 9 (7502 views)
Re: [gerble1000] here is a tricky one :~ [In reply to] Can't Post

The Date::Calc module has a function called Days_in_Month which will give you that answer.

http://search.cpan.org/~stbey/Date-Calc-6.3/lib/Date/Calc.pod

You need to supply it the year and month which can be gotten with the localtime function.

http://perldoc.perl.org/functions/localtime.html


gerble1000
User

Apr 29, 2012, 9:47 AM

Post #7 of 9 (7433 views)
Re: [FishMonger] here is a tricky one :~ [In reply to] Can't Post

ok here is what i have now
and it works

Code
my $txtnum = "3001";  
$txtnum /= 2;




my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime;
$year += 1900;
$mon += 1;
my $days = Days_in_Month($year,$mon);


only problem now is the $textnum = 1500.5 .
how do i round it to the nearest number so no decimals.
i need it to be 1500 and not 1500.5 or 1500.9 ect


FishMonger
Veteran / Moderator

Apr 29, 2012, 9:49 AM

Post #8 of 9 (7430 views)
Re: [gerble1000] here is a tricky one :~ [In reply to] Can't Post

http://perldoc.perl.org/functions/printf.html
http://perldoc.perl.org/functions/sprintf.html


gerble1000
User

Apr 29, 2012, 10:28 AM

Post #9 of 9 (7414 views)
Re: [FishMonger] here is a tricky one :~ [In reply to] Can't Post

here is what i have and it works :)

Code
my $filename = <userpass.txt>; 
my $lines = 1;
my $buffer = 0;
open(FILE, $filename) or die "Can't open `$filename': $!";
while (sysread FILE, $buffer, 4096) {
$lines += ($buffer =~ tr/\n//);
}
close FILE;

my $txtnum = $lines;
$txtnum /= 2;

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime;
$year += 1900;
$mon += 1;
my $days = Days_in_Month($year,$mon);

$days -= $mday;
$days += 1;



$txtnum /= $days;

my $rounded = sprintf("%d", $txtnum);

open(F, '>:utf8', '1.txt');
print F "$rounded";
close F;


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives