Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Changes:

  • Added error message if web page get fails
  • Use strftime instead of backtics and Windows date symbols
  • Test to see if all 3 values were found before logging
  • Using Tie::File for easy read-write access to file
  • Replace latest line with new data if Level has not changed
  • Added error message if regex could not find all 3 values
  • Replace 2 s/// with 1 tr///
  • Add math and reporting of anticipated level up
use strict; use warnings; use LWP::Simple; use URI::URL; use POSIX qw/strftime/; use Tie::File; use DateTime; my $date= strftime "%Y%m%d", localtime; # YYYYMMDD my ($url, $content, $experience, $level, $posts); $url = url('http://perlmonks.org/?node_id=844862'); $content = get($url) or die "Get failure"; $content =~ tr/\cJ\cM//d; ($experience, $level, $posts) = ($content =~ /Experience:\D+(\d+).+ Level:.+([A-Z][a-z]+\s+\(\d+\)).+ Writeups:.+>(\d+)</x); die unless ($experience && $level && $posts); my ($start, $startdt, $startexp, $today); tie my @log, 'Tie::File', 'perl_xp.csv'; ($start, $startexp) = (split ',', $log[-2])[0,1]; if ($level eq (split ',',$log[-1])[3] || '') { pop @log; } push @log, join ',',$date,$experience,$posts,$level; untie @log; my ($year,$month,$day) = $start =~ m/(\d{4})(\d\d)(\d\d)/; $startdt = DateTime->new(year => $year, month=> $month, day => $day); ($year,$month,$day) = $date =~ m/(\d{4})(\d\d)(\d\d)/; $today = DateTime->new(year => $year, month=> $month, day => $day); my $timeflies = $startdt->delta_days($today)->{days}; my $exprate = ($experience-$startexp)/$timeflies; printf "%d days, %d posts, %d points, %.2f points per day\n",$timeflie +s,$posts,$experience,$exprate; my $nextlevel = 3000; my $leveldays = ($nextlevel - $experience)/$exprate; my $leveldate = $today->add(days => $leveldays); printf "Level up in %d days, on %s\n", $leveldays, $leveldate->mdy();
1 Peter 4:10

In reply to Re^2: Storing Experience for Posterity by GotToBTru
in thread Storing Experience for Posterity by GotToBTru

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (2)
As of 2024-04-25 02:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found