It looks to me like the pieces of code relating to $process_file and $process_file2 are basically identical, which means you can simply do the same thing in a loop. Note you've got a stray . on the second-to-last line of your code, and the multiple close $process_file statements don't make sense, as those variables hold the file names, not handles.

for my $process_file ($rrconfig::locodata, $rrconfig::availlocodata) { open(my $data_file_handle, '<', $process_file) or die("$process_fi +le: $!"); my @data = <$data_file_handle>; close($data_file_handle); chomp(@data); open(my $out_file_handle, '>', $process_file) or die("$process_fil +e: $!"); foreach my $line_from_file ( @data ) { my @field = split( /\:/, $line_from_file ); next if( ($field[1] eq $delroad) && ($field[2] == $delnumber) +); print $out_file_handle $line_from_file, "\n"; } close $out_file_handle; }

Although I feel like I've been plugging it pretty often lately, note this code can be simplified using inplace editing, using either $^I (an example, but note that -i has a few caveats like in some cases only issuing a warning when something goes wrong, instead of dying), or my module File::Replace. This has the advantage of not having to load the entire file into memory, like the code above does.

use File::Replace 'replace3'; for my $process_file ($rrconfig::locodata, $rrconfig::availlocodata) { my ($infh, $outfh, $repl) = replace3($process_file); while ( my $line = <$infh> ) { chomp($line); my @field = split( /\:/, $line ); next if ($field[1] eq $delroad) && ($field[2] == $delnumber); print $outfh $line, "\n"; } $repl->finish; }

In reply to Re: Delete Lines Across Multiple Files by haukex
in thread Delete Lines Across Multiple Files by PilotinControl

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.