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

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.