Bennco99 has asked for the wisdom of the Perl Monks concerning the following question:

I am using the code from How can I find and delete a line from a file? for some fantasy football stuff. I would like know how to get verification of what was matched and deleted? So I know that only one line was removed. I would really like to match it and print what was matched in the array prior to the grep. Pardon me, I'm still a newbie at this.
use Tie::File; use warnings; my $file_name = 'G:\FF\myfantasyfootball.txt'; print "Enter the player's name that was drafted:"; my $player_drafted = <STDIN>; chomp $player_drafted; print "Player is: '$player_drafted' \n"; # open the file with tie tie my @file_lines, 'Tie::File', $file_name or die; # filter out lines containing from user input @file_lines = grep { !/$player_drafted/i } @file_lines; #print @file_lines; # close the file with untie. # IMPORTANT: always untie when you are done! untie @file_lines or die "$!";
Thank you
JB

Replies are listed 'Best First'.
Re: How do I verify what line(s) were found and deleted from a file
by dogz007 (Scribe) on Aug 16, 2007 at 22:43 UTC
    You can monitor the removal from within the grep operation, like so:

    @file_lines = grep { if (/$player_drafted/i) { print "Removing $_ from list.\n"; 0; } else { 1 } } @file_lines;
      That is exactly what I needed. Thank you! Thank you
      JB