Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Get number ranges from files internally?

by brassmon_k (Sexton)
on Dec 31, 2001 at 20:57 UTC ( #135388=perlquestion: print w/replies, xml ) Need Help??
brassmon_k has asked for the wisdom of the Perl Monks concerning the following question:

Great Monks of Wisdom I need yours cuz mine ran out,

The goal is to go through a particular file that has of course a date and time in it. The date and time are always in the same field for a regex so finding and matching is easy. The problem I'm having is I can't seem to figure out how to match data inside a file. I can match ranges for filenames but I can't seem to get the hang of telling a script okay match a number range internal to a file and grab the line. Here is what the data looks like that I'm matching on. This is an example of one line.
4478 00300 064 01 01/11/19 00:00:41 03 00000092a71228a9 255 01 00:00:3 +0 01 00 00 00 DID 0000000000000000 000 0000000000000000 0000000000000 +000 0000000000000000 000 61<BR>
All the lines look like this except different numbers etc... but the positions are the same. Now the 5th field $5 is the date of course and the $6 is the time.

What I can't figure out is let's say I had like 2000 of these lines and I wanted to get the lines from 00:01:20-00:02:20 and have the lines that were found sent to STDOUT. I can do everything but the number range. Here is my script as of now it will match time and date but only exact time and dates. That's efficient for date since most likely I would search by single days only anyway but the time well that's a problem for me. I can't conceptualize how to do this internally to file. I always just used a range idea on filenames but not inside a file. Here is my script(Of course in it's infant stage yet)....HELP!
#!/usr/bin/perl print "DateSearch = d\n"; print "Timesearch = t\n"; print "Make Your Choice."; chomp($choice = STDIN); if ($choice =~ /d/) { chomp($date = STDIN); open(FILE, "/dave/MVPTEST/mvpdoc"); while (<FILE>) { @lines = split(/\n/, $_); foreach $lines (@lines) { if ($lines =~ /(\d{4})\s(\d{5})\s(\d{3})\s(\d{2})\s(\d{2}\/\d{2}\/\d{2 +})\s(\d{2}\:\d{2}\:\d{2})/) { if ($date =~ /$5/) { print "\n@lines\n"; } } } } } elsif ($choice =~ /t/) { chomp($time = STDIN); open(FILE, "/dave/MVPTEST/mvpdoc"); while (<FILE>) { @lines = split(/\n/, $_); foreach $lines (@lines) { if ($lines =~ /(\d{4})\s(\d{5})\s(\d{3})\s(\d{2})\s(\d{2}\/\d{2}\/\d{2 +})\s(\d{2}\:\d{2}\:\d{2})/) { if ($time =~ /$6/) { print "\n@lines\n"; } } } } }
Any Help would definately be appreciated.

The brassmon_k

Replies are listed 'Best First'.
(Ovid) Re: Get number ranges from files internally?
by Ovid (Cardinal) on Dec 31, 2001 at 21:48 UTC

    brassmon_k: there are a few things that you need to do before you should start tackling issues like this. First, good formatting does not indicate good code, but poor formatting almost always indicates bad code. Further, any code over just a few lines needs to have strict and warnings turned on. There are exceptions to that rule, but you need to really apply and understand these rules before you break them. Also, try to factor out common elements in your code and then always, always, always test system calls for failure (in the code above, this is the open statement we're worried about).

    Applying these rules of thumb to your code and using a split instead of a regex, we get the following:

    #!/usr/bin/perl -w use strict; my $file = "/dave/MVPTEST/mvpdoc"; print "DateSearch = d\n"; print "Timesearch = t\n"; my $choice; do { print "Make Your Choice: "; chomp( $choice = <STDIN> ); } until ( $choice =~ /^[DdTt]/ ); chomp(my $data = <STDIN>); # this will be the date or the time open FILE, "< $file" or die "Cannot open $file for reading $!"; foreach my $line (<FILE>) { my ( $in_date, $in_time ) = (split /\s+/, $line)[4,5]; if ( ( $choice =~ /^[dD]/ && $data eq $in_date ) || ( $data eq $in +_time ) ) { print "\n$line\n"; } } close FILE;

    There are plenty of ways to clean that up from there. As for your original question, check out the Date::Calc module.

    Hope this helps and Happy New Year! :)


    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: Get number ranges from files internally?
by talexb (Canon) on Dec 31, 2001 at 21:44 UTC
    I haven't tried running your code, but a couple of stylistic matters strike me right off the bat.

    Don't read the entire file into an array -- that's slow, and it isn't necessary. Do something like

    while (<>) { next if ( $_ !~ /\d/ ); # Skip records w/o numeric fields # Match, print if within range here.. }
    instead. Much simpler.

    Also, don't try to do everything in one line -- even :) if Perl will let you do that. If I were doing this project, I would probably split the incoming data line into an array (splitting on spaces) and then deal with the individual elements. If the date matches a particular value, then I would look at the time, splitting it on the ':', comparing to the time range that you want.

    --t. alex

    "Excellent. Release the hounds." -- Monty Burns.

Re: Get number ranges from files internally?
by jlongino (Parson) on Dec 31, 2001 at 21:51 UTC
    First a few comments. You really should  use strict; and make an attempt to indent your code so that it is marginally readable to those willing to help you. You'll get quicker responses and more of them.

    Note that you should prompt the user for the time range (begin and end times). Either that or specify that they type in the begin time for a given interval (seconds, minutes, hours). The first method would be simpler to implement. I've simplified your problem but you should get the gist of it:

    use strict; my ($beg, $end) = qw(00:01:20 00:02:20); print "beg: $beg end: $end\n"; print "Matches\n--------\n"; while (<DATA>) { chomp; if (($_ ge $beg) && ($_ le $end)) { print "$_\n"; } } __DATA__ 00:01:01 01:01:01 00:01:20 00:01:21 00:02:00 00:02:21 00:02:20 00:01:19
    beg: 00:01:20 end: 00:02:20 Matches -------- 00:01:20 00:01:21 00:02:00 00:02:20


      I'll get back to you guys. I integrated what you 2 said and I got it working but I want to dress it up a bit. I'll show ya it when I'm done...It's pretty cool.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://135388]
Approved by root
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2018-06-21 01:00 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (117 votes). Check out past polls.