3 ms·
Depends on which pattern you're talking about. I was talking about inverting everything and using the magic of $_ everywhere. If you mean just the idea of usi
by Natsu 16y ago
Depends on which pattern you're talking about. I was talking about inverting everything and using the magic of $_ everywhere.
If you mean just the idea of using a hash to mark things you've seen before, though, that is very, very useful. But I would write it as:
foreach $item (@list) {
# Print every item in the list skipping duplicate items.
print $item unless $seen{$item}++;
}
Because then I don't have to think about what's happening with all the magic $_ variables Perl is tossing around. Sure, they're convenient. And I do use them some of the time. But when things break, I'm usually glad that I was explicit about what I was doing rather than relying on Perl's magic to know what I intended to do.
Maybe that's why I like Perl so much? I tend to code in as strictly disciplined a manner as I can manage, so I don't end up regretting it later.
- newman314 16y agoThe golf(ish) answer is by JadeNB. My original code is exactly the same as yours =) But more importantly, I have not seen anyone post equivalent Python or Ruby code.