
fulano
Novice
Jan 1, 2011, 9:03 PM
Post #3 of 3
(1670 views)
|
Re: [amindlessbrain] Advanced hashes
[In reply to]
|
Can't Post
|
|
Well, I can pick apart the first one, the second is giving me a headache though :). I'm going to go line-by-line, on the off chance a beginner wanders through here. First you create a temporary hash to act as a lookup for the values you've seen once already. This is going to loop through each entry in the names array 'next' jumps to the next iteration, in this case, it skips everything below and goes to the next line. This is a backwards way of writing the standard if statement, so this:is the same as this:
if ($seen{$_} == 1) {next;} These two lines only execute if $seen{$_} isn't 1, or in other words, if we've never seen this name before. They simply set $seen{$_} to one (which Perl kindly creates for us, as it shouldn't exist at this point), and pushes the current name onto @unique.
$seen{$_} = 1; push (@unique, $_); } A slightly different way of doing it would be:
my %seen = (); foreach (@names) { next if ($seen{$_}); $seen{$_} = 1; } @unique = keys %seen; #fgpkerw4kcmnq2mns1ax7ilnd open (Q, $0); while ($l = <Q>){if ($l =~ m/^#.*/) {$l =~ tr/a-z1-9#/Huh, Junketeer's Alternate Pro Ace /; print $l;}}close (Q);
|