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
In reply to Re: Perl script run foreach loop and sort without having to save and reopen the filehandle each time. (SMoP)
by tye
in thread Perl script run foreach loop and sort without having to save and reopen the filehandle each time.
by john.tm
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |