Your code looks almost OK.
My proposition is only to chomp
each line, before you
save an element in the hash.
The reason is that e.g. the last line, not terminated
with a \n
may look just the same as one of previous lines,
but without chomp
the previous line would have contained
the terminating \n
, whereas the last - not.
The resut is that both these lines will be different keys in the hash.
Compare my example program (working, presented below) with yours, there are
no other significant differences, apart from reading from __DATA__
and
writing to the console.
In my program, for demonstration purposes, I put 2 variants of printout,
one with key values (repetition counts) and another, printing just keys.
In your program leave only the second printout.
use strict; use warnings; use feature qw(say);
my %lines;
while(<DATA>) {
chomp;
$lines{$_}++;
}
while(my($key, $val) = each %lines) {
printf "%-32s / %d\n", $key, $val;
}
say '========';
foreach my $key (keys %lines) {
say $key;
}
__DATA__
10/10/2017 00:01:39:000;Sagitter
10/11/2017 00:00:01:002;Lupus
10/12/2017 00:03:14:109;Leon
10/12/2017 00:09:00:459;Sagitter
10/13/2017 01:11:03:009;Lupus
12/13/2017 04:29:00:609;Ariet
10/11/2017 00:00:01:002;Lupus
10/12/2017 00:03:14:109;Leon
Edit
Your code assigns no names to $OUTPUT_FILE
and $TMPOUTPUT_FILE
,
you even didn't declare these variables, but I assume, that in your actual
code you did it.
Another detail is that %lines
should be preceded with my
,
otherwise, as you put use strict;
the compiler prints an error.
Edit 2
There is a quicker and shorter solution than yours.
Instead of writing lines to a hash and printing them as late as in
the second step, you can do it in a single loop:
- Read the line.
- Check whether the hash already contains a key equal to the line just read.
- If not, then:
- write the line to the hash, to block the printout, if just the
same line occured again,
- print the line.
You can even write this program as a Perl one-liner:
perl -lne"print if !$lines{$_}++" input.txt
If you run the above command from the Windows cmd
, it will print the output
to the console. If you use Linux, instead of double quotes, you can use apostrophes.
You may of course redirect the output to any file, adding > output.txt
to
the above command.
The code is executed for each input line, chomped due to -l
option.
If any other details concerning Perl one-liners are not known to you, search the web.