Tips from the Basic debugging checklist: print and Data::Dumper. This will show you that $x and $y attempt to access elements beyond the end of your @rt and @pd arrays.
What should x and y be?
I ran your code thru perltidy, then fixed some split's and cluttered it up with ugly debug code to show you how I came to those conclusions:
use Graph::Undirected; use warnings; use strict; package Graph::Base; # Instantiate my $G = new Graph::Undirected; open( IN, "Input.txt" ) || die $!; while (<IN>) { chomp; my @rxn = split /\#/, $_; # "$rxn[1]\n"; #debug my @cmp = split /\s*\<=>\s*/, $rxn[1]; #use Data::Dumper; $Data::Dumper::Sortkeys=1; print Dumper(\@cmp); #print"$cmp[0]\n"; # debug first element for reactants. # my @rt = split / \+ /, $cmp[0]; # split by space and '+'. if i + don't put space b/n \+ i get bad formated output. my @rt = split /[+\s]+/, $cmp[0]; # split by space and '+'. if +i don't put space b/n \+ i get bad formated output. my $x; #declaring $x b/c the use of st +rict 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 @pd = split /[+\s]+/, $cmp[1]; #use Data::Dumper; $Data::Dumper::Sortkeys=1; print Dumper(\@pd); my $y; for ( $y = 0 ; $y <= $#pd ; $y++ ) { #print "$pd[$y]\n"; } #use Data::Dumper; $Data::Dumper::Sortkeys=1; print Dumper(\@rt); #use Data::Dumper; $Data::Dumper::Sortkeys=1; print Dumper(\@pd); #print "$x $y\n"; $x=1; $y = 1; # <----------------------------- ???????????? $G = $G->add_edge( $rt[$x], $pd[$y] ); print "$G\n"; } __END__ My output: C02=C04 C02=C04,C04=C08
Things I fixed:
In reply to Re: creating an undirected graph
by toolic
in thread creating an undirected graph
by filipo
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |