Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Unexpected deletion of data using > operator

by jonnyfolk (Vicar)
on Dec 07, 2008 at 16:47 UTC ( #728740=perlquestion: print w/replies, xml ) Need Help??
jonnyfolk has asked for the wisdom of the Perl Monks concerning the following question:

I am clearing out lines of data in a text file using a comparison between datestamps; Anything older than roughly 15 days gets deleted. Except that the line that is being deleted is not older than 15 days. Therefore I suspect that I am inadvertently deleting everything, but I know not why!!

open FH, '>', "$cuslist.tmp" or die "Can't open $cuslist.tmp: $!"; my @files; my $count; foreach my $line (@customerarray) { $count++; my ($memono, $cusref, $datestamp) = split '::', $line; if ( ($timestamp - $datestamp) > 1296000 ) { push (@files, $count); print FH "just pushed it!"; } } if (@files) { foreach my $item (@files) { $customerarray[$item - 1] = undef; } foreach my $item (@customerarray) { if ($item) { print FH "$item\n"; } } } close FH; #rename("$cuslist.tmp", "$cuslist") or die "can't rename $cuslist : $! +";

Replies are listed 'Best First'.
Re: Unexpected deletion of data using > operator
by davidrw (Prior) on Dec 07, 2008 at 17:19 UTC
    hmm .. it also looks like you're keeping track of the array indexes that you want to whack, then setting those array values to undef, then skipping over the undefs when you ouptut it. This can all be done natively w/ a simple grep.
    my @customerarray = ( ... ); my $now = time; my @keep = grep { my ($timestamp) = (split '::', $_)[2]; $now - $timestamp <= 15*24*60*60 # items less than 15 days old } @customerarray; open FH, '>', "$cuslist.tmp" or die "Can't open $cuslist.tmp: $!"; print FH "$_\n" for @keep; close FH;

    And can even collapse it further:
    open FH, '>', "$cuslist.tmp" or die "Can't open $cuslist.tmp: $!"; print FH "$_\n" for grep { time - split '::', $_)[2] <= 15*24*60*60 } +@customerarray; close FH;
      Thanks very much for showing me how to compress my code - that's really interesting. Not only that but it seems to have resolved the problem!! I don't know why - I shall have to go through it with a fine-tooth comb to find the original problem. It obviously pays to be more direct. Thanks once again...
Re: Unexpected deletion of data using > operator
by JavaFan (Canon) on Dec 07, 2008 at 17:09 UTC
    Considering that in your code fragment, @customerarray is empty (there's no code that initializes it), I'm not surprised you end up with an empty file.

    Furthermore, you don't initialize $timestamp either. Perhaps you do the initialization of the variables elsewhere in your program, but these values are essential in understanding why things go wrong. So, don't leave out that code.

Re: Unexpected deletion of data using > operator
by davidrw (Prior) on Dec 07, 2008 at 17:10 UTC
    Can you provide some sample data for @customerarray?

    my first suspicion would be that the split isn't giving you what you expect ... e.g. if the datestamp comes out undef, it would explain what you're seeing.
Re: Unexpected deletion of data using > operator
by n3toy (Hermit) on Dec 07, 2008 at 18:26 UTC
    Not initializing a variable is something that has bitten me before! And, of course I assume right away it is my code or logic and not something simple. (Usually, it ends up being a combination of both!)

    Would this snippet meet your needs?:
    foreach my $line (@customerarray) { my ($memono, $cusref, $datestamp) = split '::', $line; if ( ($timestamp - $datestamp) > 1296000 ) { next; } else { $line = $line . $line . "\n"; } } print FH "$item";
    I hope this is somewhat helpful.


      Thanks for your input, but surely bringing $item out of a hat at the end is what you were criticising me for in the first place?? :)

      davidrw's reply above (the most compressed version) removes the problem (typo not withstanding), simply by copying and pasting over my previous code. Therefore there must be a problem in that code, but I have not yet found it. Cheers.

        Whoops... Add: use MagicHat; to the top of the snippet. :-)

        This will teach me to test code before posting.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2017-08-24 03:19 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (363 votes). Check out past polls.