Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re: How to make buckets of like data

by BrowserUk (Pope)
on Feb 06, 2013 at 01:34 UTC ( #1017315=note: print w/replies, xml ) Need Help??

in reply to How to make buckets of like data

A more efficient formulation:

#! perl -slw use strict; use Data::Dump qw[ pp ]; my @array = (['A','B'],['C','D'],['A','C'],['E','F'],['F','G']); my @buckets; for( sort { $a->[0] cmp $b->[0] } @array ) { my( $x, $y ) = @$_; my $added = 0; for my $bkt ( @buckets ) { if( exists $bkt->{ $x } or exists $bkt->{ $y } ) { undef @{ $bkt }{ $x, $y }; ++$added; } } push @buckets, { $x, undef, $y, undef } unless $added; } $_ = [ sort keys %$_ ] for @buckets; pp \@buckets; __END__ C:\test>1017261-2 [["A", "B", "C", "D"], ["E", "F", "G"]]

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1017315]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (8)
As of 2017-10-21 23:44 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (271 votes). Check out past polls.