Re: Looking for regex



Chas,
Thanks for the help. I'm not expert but I think your code will output each line.

The output I'm trying to achieve.
--Output--.
h:m:s
2007-01-23 00:01:00,43.3,34.9,30.14,North,359,7,8,72,0.00,,,0.00,Wunderground v.1.13,
2007-01-23 01:11:00,42.9,34.8,30.13,North,1,5,6,73,0.00,,,0.00,Wunderground v.1.13,
2007-01-23 02:14:00,43.2,34.1,30.10,North,0,16,16,70,0.00,,,0.00,Wunderground v.1.13,
2007-01-23 03:04:00,43.2,34.1,30.11,North,0,7,12,70,0.00,,,0.00,Wunderground v.1.13,

It should cover up to 23 hrs but get the first reading for that hour.

It will read the file and parse the first temp of each hour. Unfortunately, the weather logger isn't consistent enough to use read/write at the top of the hour. The minute reading will vary.

jk

jk


"Chas. Owens" <chas.owens@xxxxxxxxx> wrote:
On Jan 26, 2008 9:38 AM, John Kufrovich wrote:
Time,TemperatureF,DewpointF,PressureIn,WindDirection
2007-01-23 00:01:00,43.3,34.9,30.14,North,359,7,8,72,0.00,,,0.00,Wunderground v.1.13,
snip
What I am trying to accomplish is pull the first temp for each hour. I have tried various combinations of regex and none seem to correctly on hr:min:sec.
snip

Out of "2007-01-23 01:11:00,42.9," you want 01 and 42.9. The first
thing to notice is that the first time a space shows up in the data is
right before what we want. This is wonderful. It will act as our
first anchor point. We then need to capture the next two characters
(the hour) then anything up to (and including) the next match a comma,
and finally capture anything up to the next comma. So, now we need to
translate that into a regex.

our anchor point: / /
capture two characters: /(..)/
match anything up to (and including) the next comma: /.*?,/
capture anything up to (but not including) the next comma /(.*?),/

Put it all together and you get: / (..).*?,(.*?),/.

You also mentioned that you only want the first temp for each hour.
Whenever you hear the words unique, first, or last, your brain should
scream "hash!":

#! /usr/bin/perl

use strict;
use warnings;

my %seen;
while () {
my ($hour, $temp) = / (..).*?,(.*?),/;
print "temp was $temp at $hour\n" unless $seen{$hour}++;
}

__DATA__
2007-01-23 00:01:00,43.3,34.9,30.14,North,359,7,8,72,0.00,,,0.00,Wunderground
v.1.13,
2007-01-23 00:06:00,43.2,34.8,30.14,North,354,11,11,72,0.00,,,0.00,Wunderground
v.1.13,
2007-01-23 00:12:00,43.2,34.8,30.14,North,1,6,9,72,0.00,,,0.00,Wunderground
v.1.13,
2007-01-23 01:11:00,42.9,34.8,30.13,North,1,5,6,73,0.00,,,0.00,Wunderground
v.1.13,
2007-01-23 01:17:00,42.9,34.8,30.12,NNW,346,3,7,73,0.00,,,0.00,Wunderground
v.1.13,
2007-01-23 01:23:00,43.1,35.0,30.12,North,359,5,11,73,0.00,,,0.00,Wunderground
v.1.13,
2007-01-23 02:14:00,43.2,34.1,30.10,North,0,16,16,70,0.00,,,0.00,Wunderground
v.1.13,
2007-01-23 02:19:00,43.2,34.1,30.09,North,0,6,13,70,0.00,,,0.00,Wunderground
v.1.13,
2007-01-23 02:24:00,43.2,33.7,30.09,North,349,8,14,69,0.00,,,0.00,Wunderground
v.1.13,
2007-01-23 03:04:00,43.2,34.1,30.11,North,0,7,12,70,0.00,,,0.00,Wunderground
v.1.13,
2007-01-23 03:09:00,43.2,34.1,30.11,North,0,10,17,70,0.00,,,0.00,Wunderground
v.1.13,

--
To unsubscribe, e-mail: beginners-unsubscribe@xxxxxxxx
For additional commands, e-mail: beginners-help@xxxxxxxx
http://learn.perl.org/





Relevant Pages

  • Re: Looking for regex
    ... We then need to capture the next two characters ... and finally capture anything up to the next comma. ...
    (perl.beginners)
  • Re: REGEX: Quotes still captured, dont want them
    ... >> double-quotes? ... > The syntax does capture the specified character sequences; ... >> 3) Why did only one of the slashes get captured? ... the characters we use to program in, being the same characters we have to ...
    (comp.lang.php)
  • Re: Todays XKCD
    ... The rest of the cartoon can be but a crude and inaccurate ... interactions between the characters, but the one for 12 Angry Men ... should capture - the gradual move of the vote from 11-1 to 0-12, ... As we enjoy great advantages from the inventions of others, ...
    (rec.arts.sf.written)
  • RE: how the print the first 7 letter of file name
    ... but the basic idea is to match & capture the first ... seven characters after the last slash character. ... So here, the basename() function strips out the path information, then I ...
    (perl.beginners)
  • Re: Finding Characters only in string of lines
    ... >I current have a batch script that executes a shell command which stdout to ... >the Windows shell a string of characters. ... >I capture the string of characters into a variable then write it into a text ...
    (microsoft.public.windows.server.scripting)