deryni has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to write a scheduler for classes that will take the class periods and print out a nice table. The input will take the form MW7, TF2, MTh1, etc. basically two day markers and then a number or one day marker and two numbers (for double periods). I have the code to take generate the table but I'm having trouble getting my code to accept the two letter thursday token.

right now I have written
@temp2 = (split /s*/, $data);
where data holds the input.
I've also tried
@temp2 = (split /([MmTtWwFf])/, $data); #gives me [ ,T ,t,H1] from input"TtH1" @temp2 = (split /([^MmTtWwFf])/, $data); #gives me [Tt,H, ,1] from input "TtH1"
my original code works for all cases except those involving the two day token for thursday.

I appreciate any help and all suggestions.

	-Etan

Replies are listed 'Best First'.
Re: need help using split()
by merlyn (Sage) on Mar 22, 2001 at 01:29 UTC
          my @split = $data =~ /\G(M|T|W|Th|F|Sa|Su|\d)/g; Th must come before T in the alternations for this to work properly. One way to write it: my @split = $data =~ /\G(Th|Sa|Su|[MTWF]|\d)/g;
Re: need help using split()
by DeaconBlues (Monk) on Mar 22, 2001 at 02:30 UTC

    Adapted from "Effective Perl Programming" p. 62. Most likely my adaption, adapted good code to bad. :-)

    while (<DATA>) { my @tok; while (s/^([A-Z][a-z]?|[0-9])//) { push @tok, $1; } print join('-', @tok), "\n"; } __DATA__ TTh7 MF4 ThF1 Results T-Th-7 M-F-4 Th-F-1
        thanks merlyn it worked.

        anyway I was originally hoping not to constrain the user to one input format.
        I tried modifying your code in a couple different ways but none of them worked.
        Any further help would be appreciated.

        Thanks again.
             -Etan
Re: need help using split()
by amelinda (Friar) on Mar 22, 2001 at 03:38 UTC
    Or you could go to the same system your school probably uses for this purpose. Replace "Th" with "R" and all is well. Since the chances of a Sunday class are very low , using "S" for Saturday is probably a reasonable choice, as well.

    Yes, I know, not a perl/split solution, but since it sounds like he's generating his own input data, why not be lazy where it's easiest?
      Arrghhh!

      Laziness is all well and good, but when you start to change the outside world in order to make your code simpler, then you're heading for problems. Forcing users to undestand 'R' to mean 'Th' is a case in point.

      The point of programming is to encapsulate the complexity in our own code (where we know(?) that we can handle it), rather than downloading it on users. That way one person (the coder) is inconvenienced, rather than the entire user community.

      Here endeth the pontification

        I thought it was pretty clear that the author was writing this just for himself. As he's the only user, why not change his perspective? Like I said, apply the principle of laziness in the most efficient manner. :grin:

        As for forcing users to use R to stand for Thursday, schools (well, tertiary, not secondary) across the country do it already. It's not exactly a sudden change. It generates a more consistent user interface (all days are one char). If you want to use multiple-char days, why not just go to all 2-char or all 3char?

        On a grand scale, putting the complexity into the program rather than in the interface is a good thing (from a user persective, at least). Yet would you truly paint all problems with the same brush?

      I had indeed thought of going to the single letter 'R' to represent thursday but decided that I'd rather spend the time and bend the program to my will instead of rethinking my input model to make programming easier. especially since it's not an important project and I'm just doing it for myself.

      but most of all I didn't do that because I don't like the letter r very much!

           -Etan