http://qs1969.pair.com?node_id=549922

SamCG has asked for the wisdom of the Perl Monks concerning the following question:

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;