remiah has asked for the wisdom of the Perl Monks concerning the following question:
Hello perlmonk.
there was data like this.
(a,b) means "a,b" are one set. (e,f) and (f,g) means "e,f,g" are one set.my @key_array = 'a' .. 'z'; my @set_array = ( ['a', 'b'], ['e','f'], ['f','g'] );
e,g,f a,b c d h i j k l ... zmy code below seems to me not smart. And in fact, I sorted the results with another script... I sorted results by their length...
use strict; use warnings; use Data::Dumper; my @key_array = 'a' .. 'z'; my @set_array = ( ['a', 'b'], ['e','f'], ['f','g'] ); #return it's id if they already exists in $set_hash #%set_hash should be like { a=>1, b=>1, e=>2, f=>2, g=>2} sub search_id { my( $set_hash, $k1,$k2)=@_; if ( exists( $set_hash->{$k1} ) ){ return $set_hash->{$k1}; } elsif ( exists( $set_hash->{$k2} ) ){ return $set_hash->{$k2}; } else { return 0; } } my (%key_hash, %set_hash, %set_hash_reverse, $id); $key_hash{$_}=0 for @key_array; #0 for initial, increment if they were + sets which has more than one item $id=1; for ( @set_array ){ my ($k1,$k2) = ($_->[0], $_->[1]); if ( exists($key_hash{$k1}) && exists($key_hash{$k2}) ){ if ( my $searched_id = search_id( \%set_hash, $k1,$k2) ){ $set_hash{ $k1 } = $searched_id; $set_hash{ $k2 } = $searched_id; } else { $set_hash{ $k1 } = $id; $set_hash{ $k2 } = $id; $id++; } #increment $key_hash{$k1}++; $key_hash{$k2}++; } else { #no set or both set. die for error; die "there is $k1 but no $k2." if ( exists($key_hash{$k1}) && (!exists($key_hash{$k2})) ) +; die "there is $k2 but no $k1." if ( exists($key_hash{$k2}) && (!exists($key_hash{$k1})) ) +; } } #{a=>1,b=>1, e=>2,f=>2,g=>2} to {1 =>[a,b],2=>[e,f,g]} for (keys %set_hash){ push @{ $set_hash_reverse{$set_hash{$_}} }, $_; } #print out for (sort {$b <=> $a} keys %set_hash_reverse){ print join(',', @{$set_hash_reverse{$_}} ), "\n"; } for ( grep {$key_hash{$_} == 0 } sort {$a cmp $b} keys %key_hash ){ print "$_\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: treat pairs, set as one item: looking for better way
by kcott (Archbishop) on Oct 19, 2013 at 07:00 UTC | |
by remiah (Hermit) on Oct 20, 2013 at 03:49 UTC | |
|
Re: treat pairs, set as one item: looking for better way
by LanX (Saint) on Oct 19, 2013 at 10:30 UTC | |
by remiah (Hermit) on Oct 19, 2013 at 11:58 UTC | |
|
Re: treat pairs, set as one item: looking for better way
by LanX (Saint) on Oct 19, 2013 at 11:59 UTC | |
|
Re: treat pairs, set as one item: looking for better way
by Anonymous Monk on Oct 19, 2013 at 06:50 UTC | |
by remiah (Hermit) on Oct 20, 2013 at 02:50 UTC |