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

Re^2: treat pairs, set as one item: looking for better way

by remiah (Hermit)
on Oct 20, 2013 at 02:50 UTC ( #1058970=note: print w/ replies, xml ) Need Help??


in reply to Re: treat pairs, set as one item: looking for better way
in thread treat pairs, set as one item: looking for better way

Hello, thanks for reply.
It took me some time to understand this.

#!usr/bin/perl use strict; use warnings; use Data::Dumper; my @key_array = 'a' .. 'z'; my @set_array = ( ['a', 'b'], ['e','f'], ['f','g'],['x','y'] ); # {a =>{a=>1}, b=>{b=>1}, ....} my %set = map {$_ => {$_ => 't'}} @key_array; for my $s (@set_array) { @{$set{$_}}{@$s} = (1) x @$s for @$s; =my_memo #same as ... for (@$s){ @{$set{$_}}{@$s} = (1) x @$s } #fill hash value at one time my %h; @h{ 1 .. 5 }= 'A' ..'F'; #(1) x @$s if @$s === (a,b) , (1) x @$s === (1,1) #vertex which has edge information .. #{ a=>{a=>1,b=>1}, b=>{a=>1,b=>1} ... =cut } my $saw = ''; print map "$_\n", grep $saw =~ s/^(?!.*$_)/$_ /, =my_memo # ??? how it can remove duplicate like "a,b","a,b" or "e,f,g","e, +f","f,g" here comes "e,f,g", $saw='', ==> $saw="e,f,g" ==> +true #saw doesn't have "" replace ^ to "e,f,g" "a,b" $saw="e,f,g" ==> $saw="a,be,f,g" ==> +true #saw doesn't have "a,b", replace ^ to "a,b "a,b" $saw="a,be,f,g" ==> $saw="a,be,f,g" = +=> false #saw have "a,b" not match; "e,f" $saw="a,be,f,g" ==> $saw="a,be,f,g" = +=> false #saw have "e,f" not match; ... removes edge already seen ... really great. =cut sort {length $b <=> length $a || $a cmp $b} #sort by length + desc || a cmp b map join(',', sort keys %$_), #"a,b" ... sort +ed values %set; # values => {a= +>1,b=>1}, {a=>1,b=>1},{c=>1},{d=>1},,,
short and great!


Comment on Re^2: treat pairs, set as one item: looking for better way
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1058970]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2015-07-05 03:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (60 votes), past polls