Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re: Positional Pattern-Matching

by BrowserUk (Pope)
on Sep 24, 2002 at 02:13 UTC ( #200276=note: print w/replies, xml ) Need Help??

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!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://200276]
[ambrus]: 1nickg: obviously the gamers don't know. but anyway, I need the whole room backlit, because watching a monitor in a dark room (like many people do) is tiring for my eyes, even more so than doing it in a lit room.
[1nickt]: ambrus I agree. I suspect one's eyes are always trying to adjust across the boundary between very bright and very dark ... like a camera's autofocus struggling to choose the target.

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (15)
As of 2017-03-27 13:58 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (320 votes). Check out past polls.