in reply to Matching multiple digits

Just to elaborate on what you're doing wrong, when you use the /g modifier on regular expression matches as in:

$stamp=~/\d{9,10}/g;

you need to use a while loop to iterate through all the matches like this:

while ($stamp =~ /\d{9,10}/g) { ... }

Besides executing your code for every match, this will also make your code work correctly in the case when there are no timestamps in the input line. This is why you are getting the uninitialized value errors and the spurious '12/31/1969' output. You are using $& in localtime and elsewhere even when the line doesn't contain any timestamps.

Replies are listed 'Best First'.
Re^2: Matching multiple digits
by skoney (Novice) on Feb 03, 2008 at 22:15 UTC
    jwkrahn:

    *Slaps hand to forehead* I should have known there'd be a module to handle this! Although, having looked at the module documentation, I have to admit I never would have come up with your solution. None of my books have anything about using the substitution regexp with two sets of braces. Thanks for the help - it ran perfectly!

    pc88mxr:

    Thanks for the explanation! Now it all makes sense. At one point I tried a  while <SOURCE> loop but it wasn't working so I abandoned that and went with what (little) I know. I'm going to go back and play with this some more so I understand the concept.

    Thank you both for helping me add another tool to my toolkit!

      None of my books have anything about using the substitution regexp with two sets of braces.

      Using braces isn't what made the solution possible. Using braces is just a stylistic choice. Using the "e" modifier after the replacement is what made the solution possible.

      s/this/that/
      is equivalent to any of these:
      s{this}{that} s(this)(that) s#this#that#

      For more on the "e" modifier, see perlre.

      If you want to learn what is possible in Perl, start reading the perldoc. Honestly it sounds silly, but take any kind of reference works to the toilet with you (like perldoc printouts), and read them whenever you are NOT trying to solve a problem. You'll gain familiarity with many features, and you'll be able to go back for more details when you need them at your desk.

      --
      [ e d @ h a l l e y . c c ]

        Thanks, halley! I'm going to look more closely at the e modifier. I think it would have helped me in another situation I had a while ago. I downloaded and printed perlre, perlop, and perlretut, and will put them in a "bathroom binder", complete with it's own highlighter so I can find the important stuff later on. I've already found something in perltut that I think will be very helpful for future reference when I'm trying to figure out what I did - the x modifier. I always thought it referred to more regexps and since I have enough trouble dealing with the basic ones I just ignored it. Who knew? Thanks for the insight!