perl:sets
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
sets [2008/07/29 08:39] – created tkbletsc | perl:sets [2009/08/13 09:57] (current) – Page moved from sets to perl:sets tkbletsc | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | * Consider finding the union and intersection of two unique arrays @a and @b: | + | Consider finding the union and intersection of two unique arrays @a and @b: |
- | | + | <code perl> |
- | foreach $e (@b) { | + | foreach $e (@a) { $union{$e} = 1 } |
- | if ( $union{$e} ) { $isect{$e} = 1 } | + | foreach $e (@b) { |
- | $union{$e} = 1; | + | if ( $union{$e} ) { $isect{$e} = 1 } |
- | } | + | $union{$e} = 1; |
- | @union = keys %union; | + | } |
- | @isect = keys %isect; | + | @union = keys %union; |
+ | @isect = keys %isect; | ||
+ | </ | ||
- | * This would be more idiomatically written as: | + | This would be more idiomatically written as: |
- | + | ||
- | foreach $e (@a, @b) { $union{$e}++ && $isect{$e}++ } | + | |
- | @union = keys %union; | + | |
- | @isect = keys %isect; | + | |
+ | <code perl> | ||
+ | foreach $e (@a, @b) { $union{$e}++ && $isect{$e}++ } | ||
+ | @union = keys %union; | ||
+ | @isect = keys %isect; | ||
+ | </ | ||
perl/sets.txt · Last modified: 2009/08/13 09:57 by tkbletsc