If it please the monks, I'll start with an explanation. Actually, this node was going to have quite different questions, but writing this node helped me sort some of them out.

I set up a sqlite database, with a users table:
my %user = ('Mouse'=>'Mickey', 'Spears'=>'Britney' .. more, see below) +; for my $lname (keys %user) { my $mail = "$user{$lname}.$lname\@acompany.com"; &add_user($dbh, $user{$lname}, $lname, $mail); }
I did a sql query in a module I wrote:
sub user_list { my ($db_ref) = @_; my $users = $db_ref->selectall_arrayref("SELECT last_name, first_n +ame from user") or die "$!\n"; return $users; }
I have no problem passing in the database handle reference, and the $users reference comes back fine. But then I have to iterate through it. It took me a while to figure out how to do this, eventually using Data::Dumper to figure out what my data structure actually looked like -- an array of arrays.
$VAR1 = [ [ 'Mouse', 'Mickey' ], [ 'Spears', 'Britney' ], [ 'Lohan', 'Lindsey' ], [ 'Martin', 'Ricky' ], [ 'Burns', 'Jeremy' ], [ 'Smithers', 'Erwin' ], [ 'Simpson', 'Homer' ], [ 'Flanders', 'Ned' ] ];

I figured out how to iterate through this with print "$user->[$_]->[0], $user->[$_]->[1]\n" for 0..5; However, I'd like to just use the last subscript of the array (a la something like $$#user). However, 0..$$#user seems to go far higher than I'm expecting. It gives me all the users, and then a couple of hundred commas (note the comma in the print statement, above). Actually, when I print $$#user alone, it gives me 3460. I'm a bit surprised it doesn't either 1) just give me all the values and stop, or 2) go on forever. I'd guess it's give me some type of exponential set. How do I get it to give me what I want (7, in this case with 8 users)?
Thanks,
Sam

update:Thank you all for the info.

I'll look into the techniques suggested by davidrw and Anonymous Monk. I agree that the hash slice is a bit more clear.

Zaxo, Leeriness appreciated. This is a sample, most of the time I'd expect to be entering users one at a time (and they're keyed by user id on the insert), so there will be no clobbering here.

The reason for the difference in variables is because some of the code is in a module, and some in script. The variables are okay, though I confess I didn't use strict in the sample code (an oversight), and this would have given me an error. Specifically, something like:

Can't use string ("4052") as a symbol ref while "strict refs" in use a +t H:\script\office.pl line 13.
I don't think I would have figured it out from this, as it pretty much tells me what I'd already determined.


-----------------
s''limp';@p=split '!','n!h!p!';s,m,s,;$s=y;$c=slice @p1;so brutally;d;$n=reverse;$c=$s**$#p;print(''.$c^chop($n))while($c/=$#p)>=1;

In reply to What way to weigh an AoA? by SamCG

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.