in reply to creating an undirected graph
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: creating an undirected graph
by choroba (Cardinal) on Aug 17, 2013 at 09:45 UTC | |
|
Re^2: creating an undirected graph
by filipo (Novice) on Aug 19, 2013 at 15:44 UTC |