#!/usr/bin/perl -w
use strict;
if (!$ARGV[0]) {
die "useage ./delete.pl [file] [options]\n";
}
my $list = $ARGV[0];
my $option = $ARGV[1];
if ($option eq '-h' or $list eq '--help') {
print "-b => backup the file\n";
die "-h || --help => this menu\n";
}
my @words;
open(LS, "$list") || die "couldnt open list\n";
foreach my $pass (<LS>) {
push(@words, $pass);
}
close(LS);
if ($option eq '-b') {
open(BK, ">$list" . '.bak') || die "couldnt open backup list\n";
foreach (@words) {
print BK $_;
}
print "File Backed up\n";
}
my %final;
foreach my $word (@words) {
$final{$word} = 1;
}
my @final;
foreach (keys %final) {
push(@final, $_);
}
open(LS, ">$list") || die "couldnt open list for edit\n";
foreach (@final) {
print LS "$_";
}
close(LS);
print "complete\n";
exit 0;
Refactorings
No refactoring yet !
Bucciarati
October 28, 2009, October 28, 2009 20:06, permalink
Your code has about 45 useless lines
perl -we '$a{$_}++ while <>; print for keys %a' file1 file2
Anthony
November 17, 2009, November 17, 2009 21:34, permalink
Can go even shorter by using list access to a hash. Its possible to assign multiple hash keys at once by doing @HASH{'key1', 'key2', 'key3} = (1,2,3).
Of course, this is still longer than just using 'sort -u'
perl -we '@a{<>} = 1; print keys %a'
Anthony
November 17, 2009, November 17, 2009 21:53, permalink
This gets down to a golf score of 20. I don't think I can get rid of the space after print...
@a{<>}.=print keys%a
Anthony
November 17, 2009, November 17, 2009 21:55, permalink
Spews warnings, but works. 15. Wish I could edit my older posts, but I'm done now.
@a{<>}.=print%a
ment to cycle through a password list, or dictionary, etc. and remove doubles, **has option of backing up file