in reply to Perl script run foreach loop and sort without having to save and reopen the filehandle each time.

If your file is not huge, you can store the lines in an array instead of a temp file (UNTESTED):
#!/usr/bin/perl use strict; use warnings; my $file = "c:\\tmp.txt"; open( my $fh, "<", $file ) or die $!; my @array; foreach (<$fh>) { chomp; if ( $_ =~ m/^\s+\d/ ) { $_ =~ s/^\s+//g; $_ =~ s/\s+$//g; $_ =~ s/\s+/,/g; push @array, "$_ \n"; } } my $OUTNET2 = "c:\\final.txt"; open my $out, '>', $OUTNET2 or die $!; my %hash; for (@array) { my $key = join ',', ( split /,/ )[ 1, 2, 3, 4 ]; print $out $_ unless $hash{$key}++; } close $out;

Note that I used your @array, which you didn't seem to be using.

  • Comment on Re: Perl script run foreach loop and sort without having to save and reopen the filehandle each time.
  • Download Code