filipo has asked for the wisdom of the Perl Monks concerning the following question:
Hi, I am fairly new to perl and can you help!. I'm trying to create a graph (undirected) below are the input, the output i want and my script thus far. I'm using split and access each element with 'for' and want to connect each compound i.e. C01=C03, C01=C04, C02=C03 and so on with R1 as the name of the edges. But i can seem to get the edges connected. I'm getting the following error message; "Graph::add_vertex: undef vertex". Thanks
Input file
R1#C01+ C02 <=> C03 + C04
R2#C01 + C04 + C05 <=> C07 + C08 + C09
input file ends
The output i want for each of the reactants to be connected to the product
C01=C03, C01=C04,C02=C03,C02=C04,C04=C07,C04=C08,C04=C09,C01=C07,C01=C08,C01=C09,C05=C07,C05=C08,C05=C09
#!/usr/bin/perl use Graph::Undirected; use warnings; use strict; package Graph::Base; # Instantiate my $G = new Graph::Undirected; open (IN, "Input.txt") || die $!; while (<IN>){ my @rxn=split/\#/,$_; # "$rxn[1]\n"; #debug my @cmp= split/\<=>/,$rxn[1]; #print"$cmp[0]\n"; # debug first element for reactants. my @rt=split/ \+ /,$cmp[0]; # split by space and '+'. if i don't p +ut space b/n \+ i get bad formated output. my $x; #declaring $x b/c the use of strict wouldn't allow me to $x or +my ($x)! for ($x=0; $x<=$#rt; $x++){ #print "$rt[$x]\n"; # debug prints the reactant compounds. } my @pd=split/\+/,$cmp[1]; my $y; for ($y=0; $y<=$#pd; $y++){ #print "$pd[$y]\n"; } $G = $G->add_edge($rt[$x], $pd[$y]); print "$G\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: creating an undirected graph
by toolic (Bishop) on Aug 16, 2013 at 15:15 UTC | |
|
Re: creating an undirected graph
by McA (Priest) on Aug 16, 2013 at 15:27 UTC | |
|
Re: creating an undirected graph
by kcott (Archbishop) on Aug 17, 2013 at 09:39 UTC | |
by choroba (Cardinal) on Aug 17, 2013 at 09:45 UTC | |
by filipo (Novice) on Aug 19, 2013 at 15:44 UTC |