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

Title:
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, details, 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, summary, 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.