in reply to string automation
Part one of your question lost me (three times!) in (sic)
" ...and add a new along with a few other changes that I will mention. There aren't any other breaks in the file so as long as it found the FIRST, skipped down ONE line, printed a new ..then that would be fine....
Please, clarify!
However, the second part -- the one you characterize as "much more complicated" -- is not.
Others may well offer better approaches, but perhaps this long-winded but simple code (with lots of prints to help you track what's happening) will be useful. Note, however, that it's only a partial solution.
#!C:/perl/bin use strict; use warnings; our $newdate = 0; our $string = '<a href="SERVER122007/intranet.htm" target="_blank">Dec + 2007</a>'; $string =~ /.+?"SERVER(\d{6}).+blank">([A-Z,a-z]{3}\s\d{4}).*/; our $ds = $1; our $monyr = $2; # changing this may require a dispatch table or a Da +te::xxx module print "\nRaw \$ds is $ds\n"; print "\$monyr is $monyr\nCode to modify this left as exercise (hint a +bove) for the OP\n\n"; if ( $ds =~ /(\d\d).*/) { our $check_yr_change = $1; if ($check_yr_change == 12) { changeyear($ds); } else { print "OK! It's not yet time to change the year\n\n"; $newdate = sprintf( '%.2x',(substr($ds,0,2) +1 ) ) . substr($ds, +2,4); print "\$newdate is: $newdate"; } } ####### sub sub changeyear { use vars qw($mo $oldyr $newyr); $mo = "01"; if ( $ds =~ /\d\d(\d{4})/) { $oldyr = $1; print "\$oldyr: $oldyr\n"; } $newyr = ( $oldyr + 1 ); $newdate= ($mo . $newyr); print "\n\$newdate in sub is: $newdate\n"; } =head OUTPUT for <a href="SERVER072007/intranet.htm" target="_blank">Jul 2007</a>: Raw $ds is 072007 $monyr is Jul 2007 Code to modify this left as exercise for the OP OK! It's not yet time to change the year $newdate is: 082007 and for <a href="SERVER122007/intranet.htm" target="_blank">Dec 2007</ +a>: Raw $ds is 122007 $monyr is Dec 2007 Code to modify this left as exercise for the OP $oldyr: 2007 $newdate in sub is: 012008 =cut
This would be somewhat shorter, were you to use Unix-ish (aka, pretty much standard, internationally) date formats; eg: YYYY-MM-DD. TTBOMK, the verbose form, "Jul 2007," is not a standard, but is common; in any case, incrementing it is less problematic.
Additional hint: Date::Manip is a fine "Leatherman" or "Swiss Army Knife." It handles an enormous range of date_time transformations and calculations, but I found it "not well suited" to this problem (or, maybe, I just didn't find the "well suited" functions) so it may repay you well to see if Date::Calc, Date::Parse and others in the Date::... family.
|
|---|