Monks,

I'm still a newbie and am having problems with arrays created from output with DBI.

I've got a table with two columns; one is an address(VARCHAR), the other is an arbitrary id number (INT). An earlier query based on the same code successfully creates an array of addresses. The bit I'm having trouble with tries to create a corresponding array of id numbers for that address array.

My, very basic, code (using strict and warnings) looks like this:

foreach $addr (@addr){ $addr_lookup = "SELECT u2addr.id FROM u2addr WHERE u2addr.addr = \ +"$addr\";"; $stm = $dbh->prepare($addr_lookup); $stm->execute; @results=(); @row=(); while (my @row = $stm->fetchrow_array){ push (@results, @row); }#end of while $stm->finish; print $results[-1]; }

Now the problem is that the elements of the array @results don't appear to have an index, so the $results[-1] throws back all the content of @result.

I've tried reworking the code to use selectcol_arrayref, as per the DBI recipes, but no change.

To make matters more frustrating, if I pull the output of the fetchrow_array into a scaler al la:

while (my ($uid_value) = $stm->fetchrow_array){ print "$uid_value, "; }#End of While
the output is individual values followed by a comma, so the database output is as expected. However, if i try and push the scaler values into an array this array also has no index and an $results[-1] throws back all the content...

I know the code I'm using is primitive but what am I missing here?

Jon

In reply to An Array Without and Index by Toppo

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.