Code Comments
Programming Forum and web based access to our favorite programming groups.How about using a hash to keep track of which things you've already handled?
my %seen;
for my $value (@values) {
next if $seen{$value}++;
# Do processing with $value here.
}
--
Keith C. Ivey <keith@iveys.org>
Washington, DC
Post Follow-up to this message>How about using a hash to keep track of which things you've already handled ? That's just the afore-mentioned count -- Free map of local environmental resources: http://CambridgeMA.GreenMap.org -- MOTD on Pungenday, the 36th of The Aftermath, in the YOLD 3173: Wibble.
Post Follow-up to this messageUri wrote: > isa > is a key valid? (not same as in a set). > ISA usually works on fixed sets of keys Well, in a strict sense it IS the same, as it is just one of many possible set operations. I suppose you could say it's a subset of sets. :-) I think you're trying to focus on the difference between sets of strings, which are the hash keys() directly, and sets of other things (e.g. objects), which are hash values(). You also emphasize the const-ness of the "isa" key set, which I think is rather too narrow. I think a better approach would be to talk about sets in general, the many set operations (including the *missing* operations, which have to be kludged in Perl), the set- related CPAN modules, and how all these things can be used. JMHO. Sorry if someone already made this point and I missed it. -- John Douglas Porter ________________________________________ ____________________________________ ________ Be a better pen pal. Text or chat with friends inside Yahoo! Mail. See how. http://overview.mail.yahoo .com/
Post Follow-up to this message>>>>> "JDP" == John Douglas Porter <johndporter@yahoo.com> writes: JDP> Uri wrote: JDP> Well, in a strict sense it IS the same, as it is just one JDP> of many possible set operations. I suppose you could say JDP> it's a subset of sets. :-) I think you're trying to focus JDP> on the difference between sets of strings, which are the JDP> hash keys() directly, and sets of other things (e.g. objects), JDP> which are hash values(). You also emphasize the const-ness JDP> of the "isa" key set, which I think is rather too narrow. JDP> I think a better approach would be to talk about sets in JDP> general, the many set operations (including the *missing* JDP> operations, which have to be kludged in Perl), the set- JDP> related CPAN modules, and how all these things can be used. JDP> JMHO. Sorry if someone already made this point and I missed JDP> it. i do plan on covering sets in their own right along with intersection and union operations too (right out of the FAQ). but i still think isa is a (sic) concept that is important enough to cover on its own. sure it is a set but a very specific type with its own name. the names of these concepts are important (almost like design patterns which i despise :). i have seen too many newbies and even some mildly experienced perl hackers don't know all these hash uses (or their names). ever see the (virtual) glassy eyes when you mention dispatch tables in polite public? :) i am aiming this for my class which is a mix of experienced hackers down to some ex-cobol coders who code perl because they get paid for it. and it is also for future classes or writings. so i want to cover as many uses at a basic level and not get into subtleties such as which are really variations of each other. they are all hash variations when you come down to that so the context and name of each use must be clearly explained. i will post here the url for these slides (with mistakes! :) when i get them done. please be kind. :) thanx, uri -- Uri Guttman ------ uri@stemsystems.com -------- [url]http://www.stemsystems.com[/url ] --Perl Consulting, Stem Development, Systems Architecture, Design and Coding - Search or Offer Perl Jobs ---------------------------- [url]http://jobs.perl.org[/url ]
Post Follow-up to this messageJerrad Pierce wrote: > That's just the afore-mentioned count True, I guess, but lots of these uses are the same. In Uri's original post, "isa" could be considered the same as "sets", and "records" the same as "data structures". With a %seen hash, the count is being calculated, but it's not being used except as a boolean, and it's a fairly common idiom. I'd say that's a separate use, but of course Uri can judge. -- Keith C. Ivey <keith@iveys.org> Washington, DC
Post Follow-up to this messageDon't know if this falls into categories already
covered (set of active objects in a class? counting?),
but I often find a hash of open filehandles useful. e.g. to
divide a big file into several smaller ones, using the
distinct values of one of the fields as filenames :
perl -MIO::File -ane '($fds{$F[1]} ||= IO::File->new(">$F[1]"))->print($_)'
< bigfile
Brian
Post Follow-up to this messageOn Friday, November 23, Uri Guttman wrote:
> one goal i have is to
> list and explain as many different uses for hashes as reasonably
> possible.
I often use them to divide one big file into several
small files named after some field in each line, e.g.
perl -MIO::File -ane '($fds{$F[1]} ||= IO::File->new(">$F[1]"))->print($_)'
< bigfile
Brian
Post Follow-up to this message>>>>> "s" == shmem <gm@cruft.de> writes:
s> You forgot an obscure corner of hashes: hashes in scalar context.
s> Here's a a use - calculate the next power of 2 of a given number:
s> sub next_power_of_two {
s> my %s;
s> @s{1..shift} = ();
s> %s =~ '/';
s> return $';
s> }
s> ;-)
GACK!!!
and that assume knowledge of the internals of hashes. not a good thing
to teach! :)
uri
--
Uri Guttman ------ uri@stemsystems.com -------- [url]http://www.stemsystems.com[/url
]
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding
-
Search or Offer Perl Jobs ---------------------------- [url]http://jobs.perl.org[/url
]
Post Follow-up to this message>>>>> "KI" == Keith Ivey <keith@iveys.org> writes: KI> Jerrad Pierce wrote: KI> True, I guess, but lots of these uses are the same. In Uri's original KI> post, "isa" could be considered the same as "sets", and "records" the KI> same as "data structures". KI> With a %seen hash, the count is being calculated, but it's not being KI> used except as a boolean, and it's a fairly common idiom. I'd say KI> that's a separate use, but of course Uri can judge. i have never run into a situation where i may process some items and later some more and need to track them with a hash. it doesn't mean that isn't a good use but i don't see why it couldn't be done differently. then again, i eschew else clauses which is rare. i just seem to code in a way that i very rarely need them. and i am not talking obscure code, just judicious use of statement modifiers, subs, logic order that generally eliminates the need for else. others will use else as they please. thanx, uri -- Uri Guttman ------ uri@stemsystems.com -------- [url]http://www.stemsystems.com[/url ] --Perl Consulting, Stem Development, Systems Architecture, Design and Coding - Search or Offer Perl Jobs ---------------------------- [url]http://jobs.perl.org[/url ]
Post Follow-up to this messageUri wrote: > i still think ISA is a (sic) concept that is important enough to > cover on its own. sure it is a set but a very specific type > with its own name. the names of these concepts are important > (almost like design patterns which i despise :). If names are that important to you, then you should use the right ones. I've never seen this set membership test thing you've illustrated called "isa". To most programmers, "isa" means something else. Oops - s/else/different/; sorry. ;-) -- jdp ________________________________________ ____________________________________ ________ Be a better pen pal. Text or chat with friends inside Yahoo! Mail. See how. http://overview.mail.yahoo .com/
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.