in reply to Perl script run foreach loop and sort without having to save and reopen the filehandle each time.
No need for two loops at all, much less a temporary file:
#!/usr/bin/perl use strict; use warnings; my $in = "c:/tmp.txt"; open( my $ifh, "<", $in ) or die $!; my $out = "c:/final.txt"; open( my $ofh, ">", $out ) or die $!; my %hash; while( <$ifh> ) { chomp; next if ! m/^\s+\d/; s/^\s+//g; s/\s+$//g; s/\s+/,/g; my $key = join ',', ( split /,/ )[ 1, 2, 3, 4 ]; print $ofh $_, "\n" if ! $hash{$key}++; }
Use while(<>) not foreach(<>) as the latter loads the whole file into RAM.
- tye
|
|---|