Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-03-28 22:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found