42306c1a1e87611519fdf1811ffe2c9c

ment to cycle through a password list, or dictionary, etc. and remove doubles, **has option of backing up file

#!/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 !

A430da3c8cb7d45bb1f3e9035e93bce5

Bucciarati

October 28, 2009, October 28, 2009 20:06, permalink

No rating. Login to rate!

Your code has about 45 useless lines

perl -we '$a{$_}++ while <>; print for keys %a' file1 file2
05b48b72766177b3b0a6ff4afdb70790

Anthony

November 17, 2009, November 17, 2009 21:34, permalink

No rating. Login to rate!

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'
05b48b72766177b3b0a6ff4afdb70790

Anthony

November 17, 2009, November 17, 2009 21:53, permalink

No rating. Login to rate!

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
05b48b72766177b3b0a6ff4afdb70790

Anthony

November 17, 2009, November 17, 2009 21:55, permalink

No rating. Login to rate!

Spews warnings, but works. 15. Wish I could edit my older posts, but I'm done now.

@a{<>}.=print%a

Your refactoring





Format Copy from initial code

or Cancel