Go Back   CodingForums.com > :: Server side development > Perl/ CGI

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-09-2012, 07:38 PM   PM User | #1
begood321
New Coder

 
Join Date: Sep 2012
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
begood321 is an unknown quantity at this point
Unhappy Delete Mulitiple lines from a file

Hey Guys,

How do I delete multiple lines with different values in an array.

Here is code below I have which works for 1 match in array if I define it but can't get it work with multiple values in an array. Foreach my $username (@remove_users) complains about match when I try wrapping it around foreach my $line (@file_lines). Thanks for your help.

Code:
   @remove_users;

    if (not defined $test_file) {
        #Make sure that the $test_file was passed in too.
        die qq(Name of log file not passed to subroutine "removeUsers"\n);
    }

    # Read file into an array for processing
    open( my $read_fh, "<", $test_file )
       or die qq(Can't open file "$test_file" for reading: $!\n); 

    my @file_lines = <$read_fh>; 
    close( $read_fh ); 

    # Rewrite file with the line removed
    open( my $write_fh, ">", $test_file )
        or die qq(Can't open file "$test_file" for writing: $!\n);

    foreach my $line ( @file_lines ) { 
        print {$write_fh} $line unless ( $line =~ /$user_remove/ ); 
    } 
    close( $write_fh ); 

    print( "User successfully removed.<br/>" );

Last edited by begood321; 10-09-2012 at 08:19 PM.. Reason: add comment
begood321 is offline   Reply With Quote
Old 10-14-2012, 05:29 PM   PM User | #2
FishMonger
Super Moderator


 
Join Date: May 2005
Location: Southern tip of Silicon Valley
Posts: 2,753
Thanks: 2
Thanked 149 Times in 144 Posts
FishMonger will become famous soon enoughFishMonger will become famous soon enough
Hello begood321,

Sorry for not replying to your question earlier.

Do you still need help with this issue? If so, please post some sample lines of your file and point out which ones need to be removed.
FishMonger is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:18 AM.


Advertisement
Log in to turn off these ads.