http://www.perlmonks.org?node_id=200276


in reply to Positional Pattern-Matching

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!