It looks like one of the elements of the array is undefined.

I'm also a little confused about what you're trying to print. For example:

SET: foreach $hit (@{$matches{$fastaseq}{$sitekey}}) {
The foreach is iterating over the elements of the array, and $hit is the value of each element, not the index. This is important later.

print "\$hit is: ",$hit,"\n";
This line prints the value of $hit. The following line, however,

print "\$hit is: ",$matches{$fastaseq}{$sitekey}[$hit],"\n";
uses that value as an index. I don't know how you populated the array, but unless the values of $hit really are index values, you're accessing possibly undefined array elements. In your example above, $hit = 6906413. Do you really have that many elements in the array?

I suspect you may be having trouble with dereferencing, but I don't know enough about the data struct you have and what you're trying to get out to give you an example. Can you post a partial dump of %matches and an example of what you want to print?

Update: If you want to iterate over the array using indices instead of the values, you can use something like

foreach my $index ( 0 .. $#array )
If you want to simply skip undefined elements in the array, you can use
next if( not defined $hit );


In reply to Re: foreach in HoHoA problems by bobf
in thread foreach in HoHoA problems by mdunnbass

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.