G'day filipo,
I see ++toolic has pointed out issues in you code. Here's my take on a solution (pm_graph_undirected.pl) which uses glob to generate the combinations.
#!/usr/bin/env perl -l use strict; use warnings; use autodie; use Graph::Undirected; my $graph = Graph::Undirected::->new(); open my $input_fh, '<', './pm_graph_undirected.txt'; while (<$input_fh>) { my ($x, $y) = /^\w+[#]([^<]+)\s+<=>\s+(.*)$/; $graph = $graph->add_edge(split /,/) for glob join ',' => map { '{' . join(',' => split /[+ ]+/ => $_) . '}' } ($x, $y); } print $graph;
Input:
$ cat pm_graph_undirected.txt R1#C01+ C02 <=> C03 + C04 R2#C01 + C04 + C05 <=> C07 + C08 + C09
Output:
$ pm_graph_undirected.pl C01=C03,C01=C04,C01=C07,C01=C08,C01=C09,C02=C03,C02=C04,C04=C07,C04=C0 +8,C04=C09,C05=C07,C05=C08,C05=C09
Update: ++choroba has pointed out a limitation with glob (below). I've tracked down details: see GLOB_LIMIT in File::Glob - POSIX FLAGS.
[Update2: very minor code change to remove an unnecessary comma s['}',]['}'] in the map. Retested and got the same output.]
-- Ken
In reply to Re: creating an undirected graph
by kcott
in thread creating an undirected graph
by filipo
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |