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!

In reply to Re: Positional Pattern-Matching by BrowserUk
in thread Positional Pattern-Matching by blink

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.