dhudnall has asked for the wisdom of the Perl Monks concerning the following question:

hello all, I am trying to automate a system that has become tedious for me. Basically I will do some simple string manipulations but I am fairly new to perl and I haven't played with it for some time. I will list a very basic example of what I would like to happen to give you an idea of how I wish for the process to work and maybe you can give me an idea of an easy way to do so.
#!/usr/bin/perl -w #tells PERL to display warnings for syntax #This section will contain the listing of variables #this is the string for the break command my $file_name = shift; #This is the file to read in my $break_name = shift; #This is the string we are looking for my $counter1=0; #This is a simple counter #This will open the main.html file to read in information open(INFILE, $file_name) or die "Can't open file\n"; while(<INFILE>){ #until we print a break until($counter1=1) { #if you see a break <br> if (/$break_name/) #if you locate a the first bre +ak { #enter onto a new line and place another break #CODE GOES HERE #increment counter to 1 so it doesn't continue + doing it $counter1++; }#end if }#end of until }#end of while loop
the file is basically editing a webpage that constantly updates logfiles for viewing. When a new month approaches I must login to this page 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. Second problem: This one is much more complicated but basically I will again read in a string except in this string I will need to update the string with the current date. The string will look something like the following:
<li><a href="SERVER072007/intranet.htm" target="_blank">Jul 2007</a>
Each month the MONTH and YEAR in both places of the string must be replaced. I didn't know of an easy way to locate that line, perhaps call a formated date function, or what. If you have any thoughts of an easy way to do this please give me your feedback thanks.

Replies are listed 'Best First'.
Re: string automation
by daseme (Beadle) on Aug 06, 2007 at 23:02 UTC
Re: string automation
by ww (Archbishop) on Aug 07, 2007 at 03:27 UTC

    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.