Perhaps some monks will decrie some inefficiencies in my code, but I think this works fairly well and has been tested:
#!/usr/local/bin/perl -w
use strict;
use POSIX; # added after observing BrowserUK's code
my (%schedule, $backup_type);
my $today = strftime( '%A', localtime ); # changed after observing Bro
+wserUK's code
while (my $line = <DATA>) {
chomp $line;
my $count = 0 ;
if ($line =~ /schedule:\s+(\w+)/i){
my $count++;
$backup_type = $1;
}
if ( $line =~ /(\w+day)\s+(\d{2}\:\d{2}\:\d{2})/i ){
$schedule{$1} = {
'backup_type' => $backup_type,
'time' => $2
};
}
}
printf "Today is %s and the backup type is %s to begin at %s\n",
$today, $schedule{$today}->{'backup_type'}, $schedule{$today}->{'tim
+e'};
__DATA__
...etc
So you're making a hash of hashrefs - the key of the parent hash is the day of the week, and then "type" and "time" are accessible through each day of the week.
Any improvements are welcomed. I may be playing fast and loose with the regex to match the day of the week, depending on what else is in your __DATA__.
--Kozz
edit: removed unneeded /g modifier from regexes, modified code to get current DOW as seen in BrowserUK's code (I learn something every day)
-
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.
|