First a few things that are nothing to do with your immediate problem, but do relate to your "sloppy code" comment:

First, always use strictures (use strict; use warnings;) Warnings (the -w switch on the "command line") are good, but strict is much better because to performs what amounts to a static analysis of your code to alert you to common errors like mismatched variable names.

Don't use subroutine prototypes (sub proto () { the () makes it a prototype). They don't do what you think and will bite you on the bum!

Don't overuse the default variable ($_). It gets clobbered all over the place so is a great source of bugs and using it doesn't help understand the code - a named lexical variable (one declared with my) is much better on both counts.

It's great to see you using three parameter open and checking the result. It would be even better if you used a lixical file handle (one declared with my) and in your error message said which file failed to open.

Ok, now to address your real problem. First off what you have commented out would only be a one shot test because by the end of the loop the file handle is stuck at the end of the file. You'd have to reset the file handle, or re-open the file to fix that. However rereading a file multiple times is bad design. Much better to read the data once into a data structure you can look up in memory. Consider:

#!/usr/local/bin/perl use strict; use warnings; my $kSkipSeconds = 3600; my %epochs; while (<DATA>) { chomp; my ($ip, $epoch) = split /,/; next if ! $epoch; $epochs{$ip} = $epoch; } my @ip_array = qw(1.1.1.1 1.1.1.3 1.1.1.2); my $now = 1320640620; # time() for my $ip (@ip_array) { if (exists $epochs{$ip} && $epochs{$ip} + $kSkipSeconds > $now) { print "Skipping $ip\n"; next } print "Time to update $ip\n"; $epochs{$ip} = $now; } print "------------ updated epochs -------------\n"; print "$_,$epochs{$_}\n" for sort keys %epochs; __DATA__ 1.1.1.1,1320639902 1.1.1.3,1320640560 1.1.1.2,1320600900

Prints:

Skipping 1.1.1.1 Skipping 1.1.1.3 Time to update 1.1.1.2 ------------ updated epochs ------------- 1.1.1.1,1320639902 1.1.1.2,1320640620 1.1.1.3,1320640560
True laziness is hard work

In reply to Re: Trouble with Array or ?? by GrandFather
in thread Trouble with Array or ?? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.