in reply to How to delete a particular line in a text file after some operation on that text file at the same time

I suggest you use module Tie::File. It allows you to treat the file just like an array, so adding, editing and removing lines is as easy as array operations.

use strict; use warnings; use Tie::File; tie my @lines, 'Tie::File, 'ListOfIpAdress.txt' or die "$!"; # define sub F such that it returns 0 if Time > 34, 1 otherwise @lines = grep { F($_) } @lines; untie @lines;
Dum Spiro Spero
  • Comment on Re: How to delete a particular line in a text file after some operation on that text file at the same time
  • Download Code

Replies are listed 'Best First'.
Re^2: How to delete a particular line in a text file after some operation on that text file at the same time
by ppp (Acolyte) on Jan 18, 2015 at 07:11 UTC
    Hii, Thanks , i tried thsi but doesnt change anything in my file.
    my $file = 'C:\shekhar_Axestrack_Intern\WindowCreation\ListOfIpAdress. +txt'; tie my @lines, 'Tie::File', $file or die "can't update $file: $!"; # define sub Fun such that it returns 0 if Time > 34, 1 otherwise @lines = grep { Fun($_) } @lines; untie @lines; sub Fun { my($line) = @_; #I do manupulation on this line to get time fr +om string # print $fho $line unless $div > 36; if( $time > 37) {return 0;} else{ return 1;} }

      You need to post a small self-contained program to demonstrate your problem. That way, we can reproduce your problem, verify that the output is wrong, and then offer ways to fix it. Unfortunately, your example code above refers to $time yet that variable is not defined in your posted code.

      Oh, and make sure that all self-contained code samples you post start with:

      use strict; use warnings;

      Update: See also SSCCE (Short, Self Contained, Correct (Compilable) Example).

      I assume then that Fun() never returns 0.

      sub Fun { $a = shift; return $a > 0 ? 1 : 0 } @a = (1,2,-3,4,-5,99); printf "%d, ",$_ for grep {Fun($_)} @a;

      Output:

      1, 2, 4, 99

      Updated to add simple example

      Dum Spiro Spero