Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Positional Pattern-Matching

by blink (Scribe)
on Sep 24, 2002 at 01:19 UTC ( #200272=perlquestion: print w/ replies, xml ) Need Help??
blink has asked for the wisdom of the Perl Monks concerning the following question:

What I'd like this code-snippet to do, is match "Schedule: Full" or "Schedule: CINC" with whatever day of week/time is associated with that particular schedule. I've hacked something together, although not elegant, that will, at least, grab the first and second match of "Schedule:". For the life of me, I can't figure out how to grab the day of week and time. Here's the code:
#!/usr/local/bin/perl -w use strict; while (my $line = <DATA>) { chomp $line; my $count = 0 ; if ($line =~ /schedule:(\s+)(\w+)/gi){ my $count++; if ($count == 1){ my $first = $2; print "first one is $first\n"; } if ($count == 2){ my $second = $2; print "second one is $second\n" +; } } } __DATA__ Schedule: Full Type: Full Backup Frequency: every 1 day Retention Level: 2 (5 weeks) Maximum MPX: 1 Residence: (specific storage unit not required) Volume Pool: (same as class volume pool) Daily Windows: Saturday 02:00:00 --> Saturday 12:00:00 Schedule: CINC Type: Cumulative Incremental Backup Frequency: every 1 day Retention Level: 2 (5 weeks) Maximum MPX: 1 Residence: (specific storage unit not required) Volume Pool: (same as class volume pool) Daily Windows: Sunday 02:00:00 --> Sunday 12:00:00 Monday 02:00:00 --> Monday 12:00:00 Tuesday 02:00:00 --> Tuesday 12:00:00 Wednesday 02:00:00 --> Wednesday 12:00:00 Thursday 02:00:00 --> Thursday 12:00:00 Friday 02:00:00 --> Friday 12:00:00

If I ran this program in it's completed state today (Monday), it would return:

"Sunday 02:00:00"

I suppose that I could populate an array with "days of week" and test "today" against the data, but I cannot figure out logic which would be able to figure out whether or not "today" is schedule "Full" or schedule "CINC"

Anyone willing to help an SA, in need of a long vacation away from backups?

Comment on Positional Pattern-Matching
Download Code
Re: Positional Pattern-Matching
by PodMaster (Abbot) on Sep 24, 2002 at 02:02 UTC
    You might wanna look into Parse::RecDescent. Anyway, something like this you ought to be able to do
    #!/usr/bin/perl use strict; use Data::Dumper; eval q{use warnings;} or local $^W=1; my @SSS = (); OUTER: while (my $line = <DATA>) { chomp $line; if( @SSS ){ if( $line =~ /Daily Windows:/i ) { while($line = <DATA>) { if ($line =~ /schedule:\s+(\S+)/i){ push @SSS, { 'Schedule' => $1 }; next OUTER; }elsif($line =~/\w+\s+\d\d\:\d\d\:/){ my( $fDay, $fTime, $tDay, $tTime ) = grep/$_/,spli +t /\s+|-->/,$line; push @{$SSS[-1]->{ 'Windows' }}, [ $fDay, $fTime, $tDay, $tTime ]; } } }else{ my($a,$b) = split /\:/, $line, 2; $SSS[-1]->{ $a } = $b; } }elsif($line =~ /schedule:\s+(\S+)/i) { push @SSS, { 'Schedule' => $1 }; } } print Dumper \@SSS; __DATA__ Schedule: Full Type: Full Backup Frequency: every 1 day Retention Level: 2 (5 weeks) Maximum MPX: 1 Residence: (specific storage unit not required) Volume Pool: (same as class volume pool) Daily Windows: Saturday 02:00:00 --> Saturday 12:00:00 Schedule: CINC Type: Cumulative Incremental Backup Frequency: every 1 day Retention Level: 2 (5 weeks) Maximum MPX: 1 Residence: (specific storage unit not required) Volume Pool: (same as class volume pool) Daily Windows: Sunday 02:00:00 --> Sunday 12:00:00 Monday 02:00:00 --> Monday 12:00:00 Tuesday 02:00:00 --> Tuesday 12:00:00 Wednesday 02:00:00 --> Wednesday 12:00:00 Thursday 02:00:00 --> Thursday 12:00:00 Friday 02:00:00 --> Friday 12:00:00
    I would guess "positional pattern matching" would somehow involve \G and pos, but that's just me (i'd call this simply parsing).

    ____________________________________________________
    ** The Third rule of perl club is a statement of fact: pod is sexy.

Re: Positional Pattern-Matching
by BrowserUk (Pope) on Sep 24, 2002 at 02:13 UTC

    I'm afraid I can't quite work out the logic by which you want to output "Sunday 02:00:00" on Monday?

    There a several obvious mistakes in your code as is.

    Re-declaring my $count++; inside the if block means that you increment a different (lexically scoped) var to the one you set to zero outside the if block. And as soon as you leave the if block, the one you incremented will disappear leaving the one outside still at zero.

    This means you will never satisfy the ($count == 2) condition on the second embedded if statement.

    You regex doesn't need the /g option as you only wish to match the pattern once. That will simple slow down the code, though that might not be a disaster in this case.

    You also don't need to capture the whitespace (\s+ not (\s+)).

    You don't need to assign $2 (or $1 if you make the previous change) to a local variable (my $first = $2; my $second = $2; in order to print out it's value. You can simply do print "....$1 or $2...);.

    I've made several other changes, using the $_ default value instead of assigning to my $line but that's just a personal preference, what you had was fine.

    As you can see, the following snippet will print Tuesday on Tuesday and Wednesday on Wednesday etc. You'll need to add whatever logic is required to decide to print Sunday on Monday yourself as I said earlier, couldn't follow the logic there, but hopefully this will get you headed in the right direction.

    It also remembers which schedule if matched the day in.

    use strict; use POSIX; my $today = strftime( '%A', localtime ); my $type = ''; while (<DATA>) { chomp; if (/schedule:\s+(\w+)/i) { $type = $1; next ; } if ( /$today\s+(\d{2}:\d{2}:\d{2})\s+-->/ ) { print "$today $1 Type: $type\n"; last; } } __DATA__ # Output C:\test>200272 Tuesday 02:00:00 Type: CINC

    Cor! Like yer ring! ... HALO dammit! ... 'Ave it yer way! Hal-lo, Mister la-de-da. ... Like yer ring!
(Kozz) Parse into hash of hashrefs
by Kozz (Friar) on Sep 24, 2002 at 02:57 UTC
    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)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://200272]
Approved by ybiC
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (8)
As of 2014-07-28 23:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (210 votes), past polls