Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Hello perlmonk.

there was data like this.

my @key_array = 'a' .. 'z'; my @set_array = ( ['a', 'b'], ['e','f'], ['f','g'] );
(a,b) means "a,b" are one set. (e,f) and (f,g) means "e,f,g" are one set.

I would like to print them per "set", and I would like to print "set which has more items" comes first.
I mean like this.
my code below seems to me not smart. And in fact, I sorted the results with another script... I sorted results by their length...
It was o.k. at that time, but I think monks have more better ways and I would like to hear your suggestions.
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"; }
I hope this makes sense. regards.

In reply to treat pairs, set as one item: looking for better way by remiah

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the leaves swirl about...

    How do I use this? | Other CB clients
    Other Users?
    Others pondering the Monastery: (3)
    As of 2018-05-26 02:45 GMT
    Find Nodes?
      Voting Booth?